Assign object that inherits UObject "cannot access private member"

Hello guys,

I created a class UTemp that derives from UObject like this (Temp.h):

#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "Temp.generated.h"

UCLASS()
class CPROJECT_API UTemp : public UObject
{
	GENERATED_BODY()
public:
  UTemp();

};

Temp.cpp:

#include "Temp.h"

UTemp::UTemp() {

}

So far, so simple. I can create objects of this class without a problem but when I try to assign them I get the following error:

  UTemp temp1;
  UTemp temp2 = temp1;



Error	C2248	'UTemp::UTemp': cannot access private member declared in class 'UTemp'

(It said something about not being able to “access the operator=” earlier but something changed and now I get this. Unfortunately I can’t remember what I did.)

I have classes inheriting AActor in this project that I can use without any problems.
Only UObject is causing those errors
It seems like there’s a problem with accessing the (private?) copy constructors of UObject

Any ideas?

Hi Alektron !

Those are UObjects, and are manages by UE memory system so you will play with there pointer instead of value

so you should do :

UTemp* temp1 = NewObject<UTemp>();//the macro create the UObject for you
UTemp* temp2 = temp1;

edit: i was pretty sure i added the class specifier, somehow i forgot, i edited the answer so it’s all good now !

1 Like

Wow, so just for completeness, I actually had to use

NewObject<UTemp>()

instead of just

NewObject()

but yeah, this actually did the trick.
Thank you so much, I’ve been trying for 2 hours now

1 Like

Thanks for the adding of the comment. This makes it much more useful to remember.

I’ve edited the answer so it’s all good now ! :slight_smile:

Cheater, cheater!
.
.
Thanks too :slight_smile: