So I have 2 classes, Bullet and Ship, that are dependent on each other, hence circular inclusion. Since I have Ship’s interface #included into Bullet’s interface, the obvious decision was to forward declare Bullet to Ship.
However, when I first tried this I still got compiler errors. I read up a bit on forward declaration and realized that I was constructing a Bullet in one of Ship’s methods, and Bullet’s default constructor is member initialized, which (and I may be wrong) wouldn’t work because a forward class declaration doesn’t allow Ship to see definitions in the interface (i.e. member initialization).
So I decided I could give up the member init and just defined the constructor in Bullet’s implementation file, however I still receive the same problem with circular dependency.
The message in particular is invalid use of undefined type struct Bullet.
I could just put the interface for Bullet and Ship in the same file, but that’s kind of a last resort. Any assistance regarding this problem is appreciated. Thanks.
Here is the spot where the error occurs:
case SDLK_UP: // Fire
{
Bullet(*this) fired_bullet; // Create bullet. Line where error occurs.
fired_bullet.Move(); // Move bullet
break;
}
Bullet’s default constructor takes an argument of the Ship that is firing the bullet, and that code is in a Ship method.
You want:
But your coupling is very tight. What does Bullet need from Ship, and what does Ship need from bullet?
I assume the bullet needs to know what ship it came from so enemy bullets don’t hurt enemy’s and vice versa. Perhaps you need a team type?
And your ships and enemies will only tell the bullet what team they are on, rather than forcing the bullet to keep track of where it came from:
About firing, maybe make a BulletManager singleton. Tell the manager you want to make a bullet at a position X, with team orientation Y, and properties Z, and the manager will take care of it for you.