Broadi
Broadi

Reputation: 3

Instance of struct inside class definition producing compiler error

In a header file I have something to the effect of:

class MoveableObject
{
   public:

    static float Gravity;
    static float JumpSpeed;
    static float MoveSpeed;

    struct State;
    struct Derivative;

    State current;
    State previous;
};

When trying to compile I get the errors:

12:9: error: field 'current' has incomplete type
13:9: error: field 'previous' has incomplete type

It's probably a very basic mistake, but I'm stumped. Thanks.

Upvotes: 0

Views: 123

Answers (2)

In the code in the question, State is a nested type inside MovableObject. To be able to create a member of type State inside MovableObject the definition of State must be inlined inside the definition of MovableObject:

class MovableObject {
public:
   struct State { ... };
   State current;
};

Upvotes: 0

Nbr44
Nbr44

Reputation: 2062

Forward declaration such as :

struct State;
struct Derivative;

Will only work for declarations if you manipulate pointers or references (because the compiler always knows the size of a pointer or a reference; however it can not guess the size of a user-defined type).

If you wish to keep your class as it is right now, you have to include the header file in which the structure State is defined.

Otherwise, switch to pointers!

Upvotes: 1

Related Questions