I assume this is from Sprite.cpp compilation.
Sprite.cpp includes sprite.h, which includes game.h at the top. The latter includes sprite.h again, which does nothing because of its inclusion protection or pragma once. This means that at this moment there is no known class called sprite - as in this compilation, it is below it.
The resulting code (after preprocessing before compilation) will look like this:
class Game { Sprite *... }; class Sprite { ... }; Sprite::func() {};
In essence, you cannot fix this easily. You will need to make one of the headers not depend on who will be included first. You can do this, every time you do not need the contents of a class, forward it, and not include it.
class Game; class Sprite {...};
and
class Sprite; class Game { Sprite *...};
so if you do this and then compile sprite.cpp, the pre-processed output will look like
class Sprite; class Game { Sprite *... }; class Sprite { ... }; Sprite::func() {};
which will work. The compiler should not know what Sprite is exactly at the time you declare a pointer to it. In fact, only the time you need a full announcement is when:
- You are using class members
- You inherit the class
- You use sizeof for class
- You create a template with it
And about that. There may be more, but they will not be common cases, and you should not quickly stumble upon them. In either case, use the preliminary declaration first, and if that really doesn't work, include the header.
dascandy
source share