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!
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).
#include
each other. This is almost always wrong.Forward-declare
class Snake
inController.hpp
, and forward-declareclass Controller
inSnake.hpp
- n.m. 2012-04-05 16:28