compilation error
Jul 6, 2009 at 8:11pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <iostream>
using namespace std;
class SpaceShip;
class SpaceStation;
class GameObject
{
public :
virtual void collide(GameObject& otherObject) = 0;
virtual void collide(SpaceShip& otherObject) = 0;
virtual void collide(SpaceStation& otherObject) = 0;
};
class SpaceStation: public GameObject
{
public :
virtual void collide(GameObject& otherObject)
{
cout <<"Inside collide of SpaceStation with GameObject" << endl;
otherObject.collide(*this );
}
virtual void collide(SpaceShip& otherObject)
{
cout <<"Inside collide of SpaceStation with SpaceShip" << endl;
otherObject.collide(*this ); //compilation error
}
virtual void collide(SpaceStation& otherObject)
{
cout <<"Inside collide of SpaceStation with SpaceStation" << endl;
otherObject.collide(*this );
}
};
class SpaceShip: public GameObject
{
public :
virtual void collide(GameObject& otherObject)
{
cout <<"Inside collide of SpaceShip with GameObject" << endl;
otherObject.collide(*this );
}
virtual void collide(SpaceShip& otherObject)
{
cout <<"Inside collide of SpaceShip with SpaceShip" << endl;
otherObject.collide(*this );
}
virtual void collide(SpaceStation& otherObject)
{
cout <<"Inside collide of SpaceShip with SpaceStation" << endl;
otherObject.collide(*this );
}
};
int main()
{}
The above code gives compilation error:
use of undefined type 'SpaceShip' - marked in above code
It should have been okay, with the forward declaration for class SpaceShip provided.
Jul 6, 2009 at 8:27pm UTC
But the actual declaration of SpaceShip does not occur until line 37, so the compiler cannot possibly compile line 28 with no knowledge of SpaceShip's declaration.
You are going to have to create a .cpp file with the implementation of some of these methods to resolve the circular dependency.
Jul 6, 2009 at 9:21pm UTC
Solved it. Thanks.
Topic archived. No new replies allowed.