Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error C2143 : missing ';' before '*'

hello I have searched everywhere on the internet for an answer but i can't find any.

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 do I fix this?

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
like image 392
Joachim Velzel Avatar asked Jul 12 '11 05:07

Joachim Velzel


2 Answers

The problem in this case appears to be that Sprite is not recognized as a type. After a better look, the problem you have is that you define:

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

in both files. You do that in the .cpp file(or Game.h file.. first code snippet) and you also do it in the Sprite.h file. The problem is that at the time that the compiler goes to Sprite.h GAME_H is already defined and therefore, thanks to the #ifndef routine it no longer compiles the Sprite.h file.

To fix it change in the Sprite.h file like so:

#ifndef SPRITE_H
#define SPRITE_H
//...
#endif
like image 172
Ioan Paul Pirau Avatar answered Oct 23 '22 13:10

Ioan Paul Pirau


I'm guessing that this is from the compile of Sprite.cpp.

Sprite.cpp includes sprite.h, which includes game.h at the top. The latter include includes sprite.h again, which does nothing due to its inclusion guard or pragma once. That means, that at that point there is no known class called sprite - as in this compilation, it's below it.

Resulting code (after preprocessing, before compiling) would look like:

class Game { Sprite *... };

class Sprite { ... };

Sprite::func() {};

In essence, you can't fix this easily. You would need to make one of the headers not depend on the other being included first. You can do that by, every time you don't need the contents of the class, to forward declare it instead of including it.

class Game;
class Sprite {...};

and

class Sprite;
class Game { Sprite *...};

so if you do this and then compile sprite.cpp, the preprocessed output is going to look like

class Sprite;
class Game { Sprite *... };
class Sprite { ... };
Sprite::func() {};

which will work. The compiler doesn't need to know what Sprite is exactly at the time you declare a pointer to it. In fact, the only times you do need the full declaration is when:

  • You use members of the class
  • You inherit from the class
  • You use sizeof on the class
  • You instantiate a template with it

And that's about it. There may be more but they won't be common cases and you shouldn't run into them that quickly. In any case, use a forward declaration first and if that really doesn't work, then include the header.

like image 41
dascandy Avatar answered Oct 23 '22 13:10

dascandy