Error C2143: missing ';' before '*'

Hi, I searched everywhere on the Internet for an answer, but I can not find.

the code:

#ifndef GAME_H #define GAME_H #include "drawEngine.h" #include "sprite.h" #include <iostream> using namespace std; class Game { public: bool run(void); protected: bool getinput(char *c); void timerUpdate(void); private: Sprite* player; // this gives me C2143 double frameCount; double startTime; double lastTime; int posx; //int posy; DrawEngine drawArea; }; #endif 

How to fix it?

sprite.h

 #ifndef GAME_H #define GAME_H #include "drawEngine.h" #include "game.h" enum { SPRITE_CLASSID, }; struct vector { float x; float y; }; class Sprite { public: Sprite(DrawEngine *de, int s_index, float x = 1, float y = 1, int i_lives = 1); ~Sprite(); vector getPosition(void); float getX(void); float getY(void); virtual void addLives(int num = 1); int getLives(void); bool isAlive(void); virtual bool move(float x, float y); protected: DrawEngine *drawArea; vector pos; int spriteIndex; int numLives; int classID; vector facingDirection; void draw(float x, float y); void erase(float x, float y); private: }; #endif 
+7
source share
3 answers

In this case, the problem is that Sprite not recognized as a type. After a better overview, the problem is that you define:

 #ifndef GAME_H #define GAME_H //... #endif 

in both files. You do this in the .cpp file (or the Game.h .. file, the first piece of code), and you also do this in the Sprite.h file. The problem is that while the compiler is switching to Sprite.h, GAME_H is already defined and therefore, thanks to the #ifndef subroutine, it no longer compiles the Sprite.h file.

To fix this, modify the Sprite.h file as follows:

 #ifndef SPRITE_H #define SPRITE_H //... #endif 
+9
source

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.

+2
source

You need to declare him as a friend in this namespace .

 class Game { friend class Sprite; public: ... 
-5
source

All Articles