Spawning a custom component in C++

I have been trying to create a custom component class in my C++ project and have been trying to spawn and add it to my custom controller class but have had no success. I have looked through the documentation and the answerhub but have yet to find an instance that uses my approach or solves my particular issue.

UCLASS()
class AMyPlayerController : public APlayerController
{
	GENERATED_UCLASS_BODY()
		
		UPROPERTY()
		TSubobjectPtr<class UMyActorComponent> CustomComponent;
};

I had no compilation issue with the above logic.

AMyPlayerController::AMyPlayerController(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	CustomComponent = PCIP.CreateDefaultSubobject<UMyActorComponent>(this, TEXT("CustomComponent"));
}

When I attempted to create the component with the above logic I ran into the following compilation errors:

error C2440: 'initializing' : cannot convert from 'UMyActorComponent *' to 'UObject *'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>          c:\program files\unreal engine\4.2\engine\source\runtime\coreuobject\public\uobject\UObjectGlobals.h(567) : while compiling class template member function 'TSubobjectPtrConstructor::TSubobjectPtrConstructor(SubobjectType *)'
1>          with
1>          [
1>              SubobjectType=UMyActorComponent
1>          ]
1>          c:\program files\unreal engine\4.2\engine\source\runtime\coreuobject\public\uobject\UObjectGlobals.h(682) : see reference to function template instantiation 'TSubobjectPtrConstructor::TSubobjectPtrConstructor(SubobjectType *)' being compiled
1>          with
1>          [
1>              SubobjectType=UMyActorComponent
1>          ]
1>          E:\Projects\CodeComponentTest\Source\CodeComponentTest\MyPlayerController.cpp(10) : see reference to class template instantiation 'TSubobjectPtrConstructor' being compiled
1>c:\program files\unreal engine\4.2\engine\source\runtime\coreuobject\public\uobject\UObjectGlobals.h(569): error C2439: 'TSubobjectPtrConstructor::Object' : member could not be initialized
1>          c:\program files\unreal engine\4.2\engine\source\runtime\coreuobject\public\uobject\UObjectGlobals.h(565) : see declaration of 'TSubobjectPtrConstructor::Object'

I have a small amount of C++ experience and have been successful in the past in getting other functionality working in my C++ project but so far this has eluded me. I was hoping someone else in the community or within Epic might have an solution to this issue or can point out something really obvious that I might be missing? Thanks

“error C2440: ‘initializing’ : cannot convert from ‘UMyActorComponent *’ to ‘UObject *’”

This error is telling you that your Player Controller does not know the actual definition of your UMyActorComponent.

Add this to your .h

#pragma once

//Add your header file here!
#include "MyActorComponent.h"

//your .generated line is here

UCLASS()
class AMyPlayerController : public APlayerController

If the compiler says it cannot find MyActorComponent.h

then add a prviate include path to where it is located, in your build.cs

PrivateIncludePaths.AddRange(new string[] { 
	"YourGameName/DirectoryOfMyActorComp.h"
});

or put it in a folder called Public

Source/YourGameName/Public

#:heart:

Rama

It is working perfectly now. Thanks so much Rama. (Your wikis and tutorial pages have been a great help on the journey as well!)