Member variable with no default constructor

Hey,

I’m facing compilation errors with the following scenario:

I have a class that inherits AActor and has a TCircularBuffer member variable and because of the way
GENERATED_BODY is setup I’m getting compilation error due to TCircularBuffer not having a default c’tor.

Declaring the class with customConstructor flag doesn’t solve the issue on its own as it seems that some functions
are still being generated under the hood that would require said buffer to have default c’tor. This can be fixed by storing the buffer as a pointer instead of by value but I personally get the feeling that it’s a quick and dirty fix and would prefer not doing so.

Sorry if this is super rookie question but I couldn’t find an answer to it.

For added detail, here’s the specific error: “error C2512: ‘TCircularBuffer’: no appropriate default constructor available”.

Tl;dr: How to handle member variables w/o default c’tor?

You can try wrapping it in a raw C++ struct or class (no USTRUCT, no UCLASS) that contains the member variable and use an initializer list.

struct MyCircularBuffer
{
public:
   
   MyCircularBuffer()
      : Buffer(1)
   { }

   TCircularBuffer<T> Buffer;
};

UCLASS()
class MyActor : public AActor
{
    GENERATED_BODY()

    MyCircularBuffer CircularBuffer;
}

And access it by using CircularBuffer.Buffer.

Just remember that if you are using any UObject derived class pointer as the circular buffer type then you have to store each pointer as a UPROPERTY somewhere or add it to the root set to prevent the object from being deleted by the garbage collector.

I’ll probably go with the class approach to be able to override certain operators, thank you for the help!
Also thanks for informing about the garbage collection, wasn’t aware of it :slight_smile: