Toomai
Toomai

Reputation: 4234

Seems like header files are being ignored/Inheritance plain not working

It's probably going to be really easy for someone to spot the problem here, but I've been punching this for over an hour and can't get a thing to work.

The code:

AIState.h

#ifndef AISTATE_H
#define AISTATE_H
#include "everythingelse.h"
class AIState {
    public:
        virtual void PieceMoved() = 0;
        virtual void Move(Player &player) = 0;
};
#endif

AIState_Generic.h

#ifndef AISTATE_GENERIC_H
#define AISTATE_GENERIC_H
#include "AIState.h"
class AIState_Generic : public AIState {
    public:
        virtual void Move(Player &player);
};
#endif

I'm getting two errors from AIState_Generic.h:

  1. error C2504: 'AIState' : base class undefined
  2. error C2061: syntax error : identifier 'Player'

I would think that including a header whose contents are nothing but the definition of AIState would stop that first error, right? The second error shouldn't be happening either (Player is defined in the "everything else" header), and putting that header right in AIState_Generic.h doesn't change anything. Forward declaration does nothing.

I even tried copying the entire AIState class into AIState_Generic.h, but that still didn't fix error 2 - and even worse, there were no errors about not implementing the pure virtual function PieceMoved().

EDIT: These two files are the only thing that could be the problem; the rest is functional legacy code. The project is in VS2008.

Moving the includes to outside the ifndefs increases the number of times the "don't know "Player"" errors appear, in addition to producing a new error:

error C2660: 'AIState::Move' : function does not take 1 arguments

...despite it being incredibly blatant that it does. It does however seem to repair the inheritance, since it produces errors about not overriding the pure virtual function.

Upvotes: 0

Views: 739

Answers (2)

Dietmar Kühl
Dietmar Kühl

Reputation: 153955

Trying these two files and declaring Player in place of the missing "everythingelse.h" it compiles fine for me. My personal suspicion is that you omitted some important details relevant to detect what the problem is. These could be

  1. the class AIState is defined in a namespace
  2. something in "everythingelse.h" defines the macro name AISTATE_H
  3. something else

To look at what is going on, I would use the omnipresent -E or /E compiler flag to see the result of preprocessing the file. This should show where AIState and Player are defined.

With respect to the other "error", i.e. the fact that the compiler is silent about PieceMoved() not being overridden is expect: you may further derive from AIState_Generic and define the function there. You will get an error if you try to create an object of type AIState_Generic.

Upvotes: 1

Elemental
Elemental

Reputation: 7521

I would guess something in everythingelse.h includes the file AIState_Generic.h - because now AISTATE_H is defined the contents of AIState.h when it is included in AIState_generic.h is excluded by the #ifndef guard.

To my mind everythingelse.h should be outside of the #ifndef (but have it's own header guards to prevent multiple compilations)

Upvotes: 2

Related Questions