开发者

Two-way inclusion of classes & template instances

I'm having a problem when trying to compile these two classes (Army and General) in their own header files:

#ifndef ARMY_H
#define ARMY_H

#include "definitions.h"
#include "UnitBase.h"
#include "UnitList.h"
#include "General.h"

class Army
{
public:
    Army(UnitList& list);       
    ~Army(void);

    UnitBase& operator[](const ushort offset);
    const UnitBase& operator[](const ushort offset) const;

    const uint getNumFightUnits() const;
    const ushort getNumUnits() const;

    const General<Warrior>* getWarrior() const;

private:
    UnitBase** itsUnits;
    uint itsNumFightUnits;
    ushort itsNumUnits;
    WarriorGeneral* itsGeneral;     
};

#endif

and

#ifndef GENERAL_H
#define GENERAL_H

#include "generalbase.h"
#include "Warrior.h"

class Army;

template <class T>
class General : public GeneralBase, public T
{
public:
    General(void);
    ~General(void);

    void setArmy(Army& army);
    const Army& getArmy() const;

private:
    Army* itsArmy;
};

typedef General<Warrior> WarriorGeneral;

#endif

I have tried forward declaring WarriorGeneral in Army.h, but it doesn't seem to work, perhaps because it's a template instance? Anyway, the errors I'm getting with the above version are several of this kind and related problems:

Army.h(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

They're not even unresolved linker problems... Note I put the typedef of WarriorGeneral in the General.h file. I don'开发者_JAVA技巧t know whether this is correct. Is there anything that can be done to make this work?

Thanks in advance!


I can't tell what Army.h line 21 is because the one you posted doesn't have that many lines. The only thing I can see that's not declared in that header is UnitList. Is it properly forward-declared or have a header include you aren't showing us?

Do generalbase.h or Warrior.h include Army.h? If so, that would cause the seemingly circular includes. Try having it not do the include but forward declare Army instead.


You can "forward declare" a template with

template <class T> class General;


With that code (both cut and pasted into the same cpp file with the general bit where the #include is) there are no errors using g++, if you have these three defined instead of #include "generalbase":

struct GeneralBase { };
struct Warrior;
struct UnitList;

Without GeneralBase:

src/Army.cpp:13: error: expected class-name before ‘,’ token

Without Warrior:

src/Army.cpp:26: error: ‘Warrior’ was not declared in this scope
src/Army.cpp:26: error: template argument 1 is invalid
src/Army.cpp:26: error: invalid type in declaration before ‘;’ token

Without UnitList

src/Army.cpp:32: error: expected ‘)’ before ‘&’ token

So it's a bit hard to see which your error is; perhaps you have defined GENERAL_H by mistake and are not including it?


Yep, @Mark B. had it right! I removed Army.h from GeneralBase.h, and it now compiles perfectly. That makes me wonder, however, what happens if I need to include Army.h in GeneralBase...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜