Error creating subclass on actor

I have an actorcomponent UEventPublisher, and I’m trying to create it in the root AActor’s constructor and take care of registering it automatically, so the component can tick and such without any extra work:

ACodeProjectCharacter::ACodeProjectCharacter()
{
    UEventPublisher* EventPublisher;
	EventPublisher = FObjectInitializer::CreateDefaultSubobject<UEventPublisher>(this, "EventObj");
}

However, this produces an error, “illegal call of non-static member function”: am I just using CreateDefaultSubobject incorrectly?

Try this:

ACodeProjectCharacter.h:

ACodeProjectCharacter(const FObjectInitializer& ObjectInitializer);
UEventPublisher* EventPublisher;

ACodeProjectCharacter.cpp:

ACodeProjectCharacter::ACodeProjectCharacter(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
 {
     EventPublisher = ObjectInitializer.CreateDefaultSubobject<UEventPublisher>(this, TEXT("EventObj"));
 }

Does your main .h include Engine.h or EngineMinimal.h? If it isn’t Engine.h, try that.

Hmm, there’s still an error from line 4 (from your paste), “ObjectInitializer: undeclared identifier”. I got the same error before when I was searching online and trying different people’s implementations- am I missing an #include maybe? I have CodeProject.h and CodeProjectCharacter.h in the cpp, and GameFramework/Character.h, EventPublisher.h, and CodeProjectCharacter.generated.h in my .h file.

Adding it to the main character.h keeps the compiler from highlighting it, but I get the same undeclared identifier error when UE compiles

Not the character.h, but whatever your ProjectName.h is called. If you haven’t changed it, there will already be a line:
#include “EngineMinimal.h”

Not in addition, replace EngineMinimal.h with Engine.h. Not sure whether that will cause problems or not, but to be safe.

Also, are you declaring EventPublisher somewhere?

Oh, I found what you mean. It included EngineMinimal.h already, I altered it to also include Engine.h with no results… it is so weird to me that ObjectInitializer is reading undefined, just to confirm, this is correct, right?

CodeProjectCharacter.h:

class ACodeProjectCharacter : public ACharacter
{
	GENERATED_BODY()

		
	/** Camera boom positioning the camera behind the character */
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
	class USpringArmComponent* CameraBoom;

	/** Follow camera */
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
	class UCameraComponent* FollowCamera;
public:
	ACodeProjectCharacter();
	ACodeProjectCharacter(const FObjectInitializer& ObjectInitializer);

CodeProjectCharacter.cpp:

	ACodeProjectCharacter::ACodeProjectCharacter()
{
	// Set size for collision capsule
	GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);

	EventPublisher = ObjectInitializer.CreateDefaultSubobject<UEventPublisher>(this, TEXT("EventObj"));

Just adding this to clarify, I can mouseover ObjectInitializer in the .h, and the popup correctly identifies it as “const FObjectinitializer &ObjectInitializer”, so I know it’s getting typed correctly, but cutting & pasting “ObjectInitializer” from that line to the .cpp still returns the unknown type error.

Same undeclared identifier error on ObjectInitializer- what’s weirding me out is that this is a brand-new project, the only change to the base state was creating EventPublisher, and that doesn’t interact with anything.

CodeProjectCharacter.h has an #include for EventPublisher.h, and I declare it in the character’s header.

Hmmm. Copy/paste the exact error you are getting and the line the error lists.

Sure, here’s the class constructor in the .cpp the error is listing. The exact error is "Error MyFilePath\CodeProject\Source\CodeProject\CodeProjectCharacter.cpp(16) : error C2065: ‘ObjectInitializer’ : undeclared identifier
"

The listed line, 16, is the one that starts “EventPublisher = ObjectInitializer.CreateDefaultSubobject”

#include “CodeProject.h”
#include “Engine.h”
#include “CodeProjectCharacter.h”

ACodeProjectCharacter::ACodeProjectCharacter()
{
	GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);

	EventPublisher = ObjectInitializer.CreateDefaultSubobject<UEventPublisher>(this, TEXT("EventObj"));

//A bunch of control and movement initialization that came with the class follows
}

ACodeProjectCharacter::ACodeProjectCharacter()

should be:

ACodeProjectCharacter::ACodeProjectCharacter(const FObjectInitializer& ObjectInitializer)

Ooh okay, that worked- sorry to waste your time with such a silly oversight, and thank you!! :slight_smile:

Glad I could help. If you accept the answer and mark the question resolved it will be helpful to anyone searching about the same issue in the future.

That’s weird, I thought I’d accepted when I made the last comment… in any event I hit it again, thank you again for the help :slight_smile: