views:

71

answers:

4

Hi all, 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't know whether this is correct. Is there anything that can be done to make this work?

Thanks in advance!

A: 

You can "forward declare" a template with

template <class T> class General;
Alexandre C.
A: 

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?

Pete Kirkham
+1  A: 

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.

Mark B
Updated the code to include the whole .h files, sorry!. UnitList is included. generalbase.h does include Army.h... let me see, thanks!
Kristian D'Amato
@Kristian: How about telling us exactly what line 21 is rather than making us count?
David Thornley
A: 

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...

Kristian D'Amato
You won't arbitrarily need to include Army.h; you will have a specific reason why you'd want to. Without knowing what that reason would be, we can't help you. All we can say is that you should be able to figure out how to get what you want without the include.
David Thornley