Incomplete types/ forward declaration / compilation understanding needed

Go To StackoverFlow.com

0

I have a Snake class, which has a pointer to a Controller class object. This pointer will be assigned some new data, an object that is a derived from controller (eg AIController), at runtime.

I need Controller objects to be passed a pointer to Snake when they are constructed so that i can call Snake Getters/Setters within the controller class.

On the line, in the Snake constructor, flagged in the snippet below i get the following errors:

In constructor 'Snake::Snake()':|
error: expected type-specifier before 'PlayerController'|
error: cannot convert 'int*' to 'Controller*' in assignment|
error: expected ';' before 'PlayerController'|

Snippet:

Snake::Snake() : _xVelocity(0), _yVelocity(0)
{
   _controller = new PlayerController(this);
   Initialise();
}

Snake is defined like this:

class Controller;

class Snake
{
   public:
      Snake();
      virtual ~Snake();
...

   private:
      ...
      Controller* _controller;


};

Controller like this:

#include "Snake.hpp"

class Controller
{
     public:
         Controller(Snake* s);
         ~Controller();


     protected:
         ...
         Snake* _s;
};

and PlayerController like this:

#include "Controller.hpp"

class PlayerController : public prg::IKeyEvent, public Controller
{
     public:
         PlayerController(Snake* s);
         ~PlayerController();


     private:
         virtual bool onKey (const prg::IKeyEvent::KeyEvent& key);

};

I'm not certain that my attempt use a pointer to a controller object so that i can allocate the snake different controllers at runtime is correct and i know that somethings not quite right with my use of forward declaration. I appreciate the initial responses and hope that providing the errors will allow you to give me some more information. I will continue to attempt to clean up the problem so that i can properly understand the compilation process but in the mean time i'd really appreciate any help!

2012-04-05 16:17
by Holly
You have two header files that #include each other. This is almost always wrong.

Forward-declare class Snake in Controller.hpp, and forward-declare class Controller in Snake.hpp - n.m. 2012-04-05 16:28

It would be easier if you could provide a minimal example code that demonstrates your problem, so we could compile it and try to change (implement classes that don't do anything). Sometimes, when trying to write a minimal code that reproduces the problem it gets easier to see what's wrong and you could end up discovering how to fix it yourself... You could also post the compiler warnings you're seeing.. - pzanoni 2012-04-05 16:29
The error in Snake::Snake() is (most probably) in Snake.cpp. Did you include Controller.hpp in Snake.cpp - selalerer 2012-04-05 18:03
@selalerer Thanks, i forward declared Controller in Snake.hpp.. i was able to get it all to compile by including PlayerController in Snake.cpp. which might have been what i was missing! but i'm not convinced my overall structure is appropriate. Still I guess it'll do till i can get some more opinions on it : - Holly 2012-04-05 18:13


0

Put the forward declarations in the header it is needed not before including the header. If you put it before including the header, you have to do it each time before you include the headers.

Another thing that seems to be missing here is the duplicate inclusion guards (either with #ifndef or with #pragma once).

In the future it is best to put here the actual errors you are encountering (the compiler's error messages).

2012-04-05 16:34
by selalerer
Thanks for the advice, sorry i have included a few more details now. I do have inclusion guards on each hpp, i just didn't paste them to try and keep my question tidy. Will bear that in mind in the future - Holly 2012-04-05 17:49
Ads