-1

Possible Duplicate:
error: expected class-name before ‘{’ token

I've got one main super-class GameObject and derived class GuiBitMapFont. It always throws expected class-name error. But if I will add forward derivation in GuiBitMapFont class GameObject; it throws invalid use of incomplete type 'class GameObject' and forward declaration of 'class GameObject'.

EDIT Yes there was #include GuiBitMapFont in GameObject file. But that was my mistake while writing this question. Compiler still throws those two errors.

#ifndef GAMEOBJECT_H
#define GAMEOBJECT_H

#include <string>
#include "Texture.h"

class GameObject {
private:
    int x;
    int y;
    int width;
    int height;
public:
    GameObject();
    GameObject(int x, int y, int width, int height);
    GameObject(const GameObject& orig);
    virtual ~GameObject();

    virtual void draw();
    virtual void update();

    //ignore those, i need to rewrite it....
    void setX(int x);
    void setY(int y);
    void setWidth(int width);
    void setHeight(int height);
    int getX() const;
    int getY() const;
    int getWidth() const;
    int getHeight() const;
};

#endif  /* GAMEOBJECT_H */

and derived

#ifndef GUIBITMAPTEXT_H
#define GUIBITMAPTEXT_H

#include <string>
#include "SDL.h"
#include "GameObject.h"
#include "BMF.h"

class GuiBitMapText : public GameObject { //error: expected class-name before '{' token
private:
    std::string text;
    BMF *font;

    //SDL_Surface *surf;
    SDL_Texture *texture;
public:
    GuiBitMapText(int x, int y, std::string text, BMF *font);
    GuiBitMapText(const GuiBitMapText& orig);
    virtual ~GuiBitMapText();

    virtual void draw();
    virtual void update();
};

#endif  /* GUIBITMAPTEXT_H */
Community
  • 1
  • 1
Octopussy
  • 128
  • 2
  • 10
  • @Zaraka there used to be `#include GuiBitMapText.h` at the beginning of GameObject.h, and all the answers and comments correctly point out that this 'include' shouldn't be there. But you seem to have removed it in a quick edit. Why? – jogojapan Dec 27 '12 at 13:35
  • Because It shouldn't be there it was commented and without that line it still throwing same errors – Octopussy Dec 27 '12 at 13:37
  • 1
    Please put a comment in the **current** source of this post denoting the *exact* line where the error still happens, now that you changed the question code and effectively invalidated every previously correct answer. – WhozCraig Dec 27 '12 at 13:40
  • @Zaraka and while you are at it, could you also remove the functions labeled "ignore those, I need to rewrite them", and possibly all includes, types and data members that are not required to reproduce the problem? It may be a bit of work, but it will help you and the SO community to understand the problem better. – jogojapan Dec 27 '12 at 13:43
  • @jogojapan I've commented everything in GameObject except Constructors and Destructor... still same errors – Octopussy Dec 27 '12 at 13:49
  • Based on your code, look in SDL.h and see if you fence-posted it with `#ifndef GAMEOBJECT_H` on accident. Likewise with `Texture.h`. The only way I see this not compiling is if `GameObject.h` is clipped out via internal fenceposts or an erroneous `#define GAMEOBJECT_H` in an unrelated header file. Since `SDL.h` and `Texture.h` are the only non-system headers potentially included prior to the class def, I'd check them. – WhozCraig Dec 27 '12 at 13:51
  • I have nowhere else in my project define GAMEOBJECT_H I will try to look in SDL headers – Octopussy Dec 27 '12 at 13:52
  • @WhozCraig No, there isn't in any file (my or SDL library) #define GAMEOBJECT_H anywhere else than in GameObject.h – Octopussy Dec 27 '12 at 14:01
  • Ok, a comment rather than a proper answer, but GameObject need not contain any of those includes at all, not even string or Texture.h. GuiBitMapText needs to include GameObject and string but not any of the others, which could instead be forward declarations. – CashCow Dec 27 '12 at 14:04
  • @Zaraka then the only other plausible explanation I can muster is your including header files different than the ones you think you are. Put a `#error` inside your `GameObject.h` header file *inside* the fencepost and compile any single source file that includes `GuiBitmapText.h` and **not* `GameObject.h`. If your compile doesn't break on that #error you're not including the header files you think you are. – WhozCraig Dec 27 '12 at 14:09
  • @WhozCraig Yes, I just figured it out. – Octopussy Dec 27 '12 at 14:14
  • @Zaraka with the fixes you have made has your problem gone away yet? – CashCow Dec 27 '12 at 14:15
  • @Zaraka see my answer too and change those includes to forward declarations, and try doing this in as many headers as you can. – CashCow Dec 27 '12 at 14:16
  • @CashCow Yes, the problem is now fixed. Fine I will try to rewrite it. I'm getting kinda lost with 20 headers file... – Octopussy Dec 27 '12 at 14:21

4 Answers4

4

You have a circular include. Remove this line from GameObject.h:

#include "GuiBitMapText.h"

You don't use this class in GameObject.h, so there is no need for the include to even be there. There are cases where you have to forward-declare classes when dealing with types whose definitions reference each other, but since GameObject does not make any reference to GuiBitMapText, there is no reason you should need to forward-declare in this instance.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • That was Typo, it shouldnt be there. Ignore it, it still throws same errors – Octopussy Dec 27 '12 at 13:34
  • 1
    I don't see how it *could* give you that error if you did remove that include line, unless there is more to these two files that you are not showing us. – cdhowie Dec 27 '12 at 13:36
  • I only use GameObject in diferent file. But everything else is here. And yes I'm sure. – Octopussy Dec 27 '12 at 13:39
  • @KillianDS nothing related to GameObject or GuiBitMapFont. – Octopussy Dec 27 '12 at 13:45
  • The error you are getting is that a class-name is expected before `{` - this means that you are not declaring a `class` before it's being used in some way or another. It's 99% sure to be a "include files problem". – Mats Petersson Dec 27 '12 at 13:47
1

You have a cyclic inclusion. Think about when something that includes GameObject.h (such as GameObject.cpp) is being compiled. GameObject.h will be included, which then includes GuiBitMapText.h above the definition of GameObject, which then includes GameObject.h above the definition of GuiBitMapText. However, your include guards will stop this last inclusion from actually doing anything, so GuiBitMapText won't be able to compile because GameObject is not defined before it.

But GameObject doesn't even depend on GuiBitMapText, so there's no reason to have #include "GuiBitMapText.h" there. Just get rid of it and you'll be fine.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
0

You need to understand header dependency properly.

You should learn when to actually include a header and when to use a forward declaration.

In the case of GameObject, it actually has no external dependencies at all. Your header file need not include any other headers whatsoever.

In the case of GuiBitMapText:

  • It is a type of GameObject. To derive from a class you must include its header (You have). Therefore keep the #include GameObject.h
  • Your class has a string as a member. So you need the header of <string> too
  • Your class has a pointer to a BMF. A forward declaration will suffice. Removing the include might break some code somewhere that was expecting this include to be there but fix that code, not this file. Replace #include "BMF.h" with class BMF;
  • Your class has a pointer to SDL_Texture. Not only that, but this pointer only appears in the private section of the class and doesn't appear in the public interface at all. (Unlike the BMF pointer which appears in a public constructor too as well as a class member). So even more so here you should use a forward declaration. Then users of your class do not get the overhead of that include. Replace "#include SDL.h" with class SDL_Texture;

This is something useful to learn and will help you in the future, as well as in this particular case.

Having changed your headers to use only forward declarations, the compilation units relating to these files, i.e. the .cpp files, will now have to include the header.

CashCow
  • 30,981
  • 5
  • 61
  • 92
  • The reason why GameObject has those include is becase it has string variable a texture variable too, later I thought those two variables are necessary and deleted them, but forgot to delete those includes... I changed everything except SDL.h, because SDL.h is C-library with functions I use, so I would not have access to them. – Octopussy Dec 27 '12 at 14:28
  • If SDL_Texture is a struct rather than a class then put struct in your forward declaration but still use a forward declaration in your header. In your compilation unit, i.e. your .cpp file you then include the header. – CashCow Dec 27 '12 at 14:31
  • But now. I cant work with BMF class in GuiBitMapText.cpp file. When I try use some class method. Compiler throws "invalid use of incomplete type 'class BMF'" – Octopussy Dec 27 '12 at 14:32
  • They told me my .cpp files should have not include anything else except their header file... I'm confused... – Octopussy Dec 27 '12 at 14:37
-1

The error was trigger by another file where I had this two lines

#include "GameObject.h"
#include "GuiBitMapText.h"

I really didn't need to Include GuiBitMapText in that file, so I deleted include GuiBitMapText and now it works...

Octopussy
  • 128
  • 2
  • 10
  • This is explicitly stated in several other answers, that it is an other file than those posted is a mere detail (fit for a comment). You should remove this and accept one of the other answers – KillianDS Dec 27 '12 at 14:23