user1928543
user1928543

Reputation: 45

expected class-name before ‘{’ token C++

I have a trouble with my c++ code. I know there is much advices with error "expected class-name before ‘{’ token" but I still can't find where I have it. Here is my sources:

Postava.h

#include <exception>
#include <string>
using namespace std;

#ifndef __Postava_h__
#define __Postava_h__

#include "Barva.h"
#include "Pozice.h"
//#include "Budova.h"
//#include "HerniEngine.h"
#include "GrafickyObjekt.h"

class Budova;
class HerniEngine;
//class GrafickyObjekt;
class Postava;
struct Barva;
struct Pozice;

class Postava:public GrafickyObjekt{            //<----- Here is the error
private:
    std::string m_jmeno;
    int m_nosnost;
public:
    Postava(std::string jmeno, int nosnost);
    Budova* m_Budova;
    HerniEngine* m_HerniEngine;
    std::string vratJmeno();
    int vratNosnost();
    void vykresli();
};

#endif

GrafickyObjekt.h

#ifndef __GrafickyObjekt_h__
#define __GrafickyObjekt_h__

#include "HerniEngine.h"
#include "Pozice.h"
#include "Posun.h"

class HerniEngine;
class GrafickyObjekt;
class Scena;
struct Pozice;
struct Posun;
class HerniEngine;

class GrafickyObjekt {
protected:
    Pozice  m_pozice;
public:
    HerniEngine* m_HerniEngine;
    // kazdy potomek, tj. graf. obj. ma pozici
    GrafickyObjekt(Pozice pozice);
    // vsichni potomci ji musi implementovat
    virtual void vykresli() = 0;
    // tyto metody nejsou (ciste) virtualni, budou normalne zdedeny
    // tim mam zaruceno, ze vsichni potomci je maji
    void pohni(Posun posun);
    void pohni(Pozice pozice);
};

#endif

Sorry for my english and for names of classes and names of variables, it's in czech.

Thanks a lot for every advice.

Upvotes: 0

Views: 1255

Answers (2)

AnT stands with Russia
AnT stands with Russia

Reputation: 320391

Same answer as in all similar questions asked before:

You created a circular include sequence

It is not obvious from what you posted so far (since you haven't posted all headers). But it is a safe bet that your other header files taken together must produce a circular include "path". More precisely, your GrafickyObjekt.h somehow indirectly includes Postava.h (through other header files that you haven't posted).

The include guards you used in your header files will "break" that cycle in some unpredictable or (better word) unforeseen way. In your case the include guards caused Postava.h to get physically included first, which is why it knows nothing about GrafickyObjekt even though it seems to explicitly include GrafickyObjekt.h. Hence the error.

Circular includes make no sense and achieve nothing. You have to stratify your headers by levels - from low level to high level - and make sure that higher-level headers include lower-level headers, but never the other way around.

Once you achieve that sort of order, you can proceed to resolving circular data dependencies by introducing forward class declarations. I see that you already tried to do that and ended up with a total catastrophic mess, where you basically forward-declare all classes in all headers. Get rid of that mess and start over, by fixing the include stratification first.

Upvotes: 6

elcuco
elcuco

Reputation: 9208

It seems that some of your previous includes are faulty.

#include "HerniEngine.h"
#include "Pozice.h"
#include "Posun.h"

Upvotes: 1

Related Questions