7

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
Ioan Paul Pirau
  • 2,733
  • 2
  • 23
  • 26
Joachim Velzel
  • 657
  • 2
  • 9
  • 21
  • 8
    We probably need to see the content of `sprite.h" to comment intelligently on this. – Jerry Coffin Jul 12 '11 at 05:47
  • may be `Sprite` class in under a namespace? – Donotalo Jul 12 '11 at 05:50
  • 1
    The error means that `Sprite` isn't the name of a class in the global namespace. Either `Sprite` isn't declared anywhere (maybe you have a typo in `sprite.h`?), or it's declared in a namespace, in which case you need to fully qualify it as `TheNamespace::Sprite` or add a `using` declaration. – Adam Rosenfield Jul 12 '11 at 06:10
  • 3
    @Joachim Velzel: Do not use `using namespace std`, especially in a header file! – Praetorian Jul 12 '11 at 06:21

3 Answers3

10

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
Ioan Paul Pirau
  • 2,733
  • 2
  • 23
  • 26
  • 1
    For anyone who experienced a similar error and came here via google: A circular inclusion (in this case if `Sprite.h` also included `Game.h`) can cause this, too. Because then the definition of `Game::player` can be reached before the definition of `Sprite`, `Sprite` is again not recognised as a type. – iFreilicht Nov 30 '14 at 15:32
3

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.

dascandy
  • 7,184
  • 1
  • 29
  • 50
-5

You need to declare it as a friend in that namespace.

class Game
{
    friend class Sprite;

public:
   ...
Community
  • 1
  • 1
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • No s/he doesn't unless the situation OP has is same as the other problem, I set a bad example here by speculating & realizing that I deleted my answer. Speculations can lead a newbie to make certain wrong assumptions. – Alok Save Jul 12 '11 at 05:54
  • I speak from experience, having tripped over section 7.3.1.2 myself previously. – Ignacio Vazquez-Abrams Jul 12 '11 at 05:58
  • There is no need for privileged access in what's posted, probably there is just something like an error in `sprite.h`. – Georg Fritzsche Jul 12 '11 at 06:00
  • Ofcourse, Your experience is invaluable and most likely the problem OP has same is the same as you mentioned but if it is not OP is going to make some unwarranted assumptions. Just to add it is not my downvote. Not the one in to business of downvoting. – Alok Save Jul 12 '11 at 06:02
  • You really don't need friend declarations to be able to declare a pointer, unless it's a private inner class of another class. And if that, then you need to be friends with that class, not sprite. – dascandy Jul 12 '11 at 06:16
  • @Ignacio Vazquez-Abrams: `Sprite` would need to be `Game`'s friend only if it needs access to the latter's private members. Nothing in the posted sample indicates that this is the case. – Praetorian Jul 12 '11 at 06:19
  • Now it says that 'Sprite' has no constructors. But it does in sprite.h – Joachim Velzel Jul 12 '11 at 06:51
  • @Joachim Velzel: Unless you post the source code for `Sprite.h`no one can tell you what the problem is. What you will get are wise/experienced guesswork, so post your source code for `sprite.h` – Alok Save Jul 12 '11 at 06:55