Trouble initializing UCLASS with class member in default constructor.

I have this error
error: constructor for 'UActionQueue' must explicitly initialize the member 'queue' which does not have a default constructor

Here is the code in question

// ActionQueue.h
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class CPPTEST_API UActionQueue : public UActorComponent
{
	GENERATED_BODY()
	TFragmentQueue<FActionStruct> queue;

public:	
	// Sets default values for this components properties
	UActionQueue();

protected:
	// Called when the game starts
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
};

// ActionQueue.cpp
UActionQueue::UActionQueue() 
: queue(1,1)
{
	PrimaryComponentTick.bCanEverTick = true;

	// ...
}
template<class T>
class CPPTEST_API TFragmentQueue
{
private
  // member variables ...
public:
    TFragmentQueue(int queuedSize, int previousSize)
    {
        // init code
    }
    
  // rest of class code ...
}

So as far as I know this is valid C++ but Unreal has some problem with it and I don’t get why. I’m trying to initialize queue in the default constructor of UActionQueue but I just get this error. Can I not have objects without default constructors? I’ve looked through the Gameplay Classes and Epic C++ Coding Standard articles and saw UObjects need default constructors but TFragmentQueue is not a UObject, its just a basic class.

I’ll mark this as the answer if nobody has anything to add but what worked is moving the assignment of queue into the declaration in the header.

UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class CPPTEST_API UActionQueue : public UActorComponent
{
	GENERATED_BODY()
	TFragmentQueue<FActionStruct> queue = TFragmentQueue<FActionStruct>(1,1);

public:	
	// Sets default values for this components properties
	UActionQueue();

};

I still don’t see why I cant use the constructor since that’s what it is for and documentation says to use it for that but whatever.

Where was the error reported?

While everything you’ve written is valid C++, you have to remember that Unreal generates code for your class as well. I suspect that the error is in a generated constructor that you don’t have any control over.

An alternative would be to have a default constructor and a way to set those separately. But your header based solution works too in this case.

Here is the full error
CompilerResultsLog: /home/user-name/Documents/Unreal Projects/CPPTest/Intermediate/Build/Linux/UnrealEditor/Inc/CPPTest/UHT/ActionQueue.gen.cpp:553:31: error: constructor for 'UActionQueue' must explicitly initialize the member 'queue' which does not have a default constructor

That “.gen.cpp” makes me think you’re right about the generated code. If that’s the case what options do I have?

The only solutions I can think of are the two I gave in my previous post. Either keep doing what you’re already doing or add a default constructor. Since you’re dealing with a template and it can’t be a UPROPERTY you have slightly more flexibility than you might otherwise.

1 Like