zeluisping
zeluisping

Reputation: 224

C++ Forward Declaration (Pointer) - Access member

I'm working with OpenGL and DirectX, and I've started developing the basics for the object-oriented game classes. The structure of the current classes is as follows:

Object
---|---Actor
---------|---Pawn
---------|---Controller

So I have the Pawn and Controller class that inherit Actor which inherits Object.

The problem is that I need to have a reference of Controller in the pawn class and an instance of Pawn in the controller class.
Because of this I forward-declared Pawn and Controller in Actor.h:

// Actor.h

// (...) Actor Declaration (...)

class Pawn;
class Controller;

And then in Pawn.h:

// Pawn.h

class Pawn : public Actor
{
private:
    Controller* controller;
public:
    void DoSomethingWithController(void);
}

This is all good, no errors and all. The problem is when I want to access the members in that class:

void Pawn::DoSomethingWithController(void)
{
    // this->controller-> can't access members like this (members not found)
}

So, what should I do to be able to have a pointer of Controller in Pawn and a pointer to a Pawn in my Controller keeping them in different files (.h and .cpp) and at the same time being able to access it's members?

Thank you for your time. :D

[if more information is required I'll provide it]

Upvotes: 0

Views: 2524

Answers (1)

klement
klement

Reputation: 184

Forward declaration says to compiler that given type will be provided further. Compiler doesn't have a clue about fields or members of that type. Therefore you need to include corresponding .h file in your .cpp file (the place you access the members of "controller" class).

Upvotes: 5

Related Questions