0

I'm realitivly new to c++ and I'm trying to complete a small project to understand inheritance. I'm having problems with includes and forward declarations. Here are the following headers that seems to be at issue:

player.h:

#ifndef PLAYER_H
#define PLAYER_H
#include "abstractPlayerBase.h"
#include "cardException.h"
class abstractPlayerBase;
class Player: public AbstractPlayerBase
{
   ...
   //a function throws a CardException
};
#endif

baseCardException.h:

#ifndef BASECARDEXCEPTION_H
#define BASECARDEXCEPTION_H
#include "Player.h"

class BaseCardException
{
...
};
#endif

cardException.h:

#ifndef CARDEXCEPTION_H
#define CARDEXCEPTION_H
#include "baseCardException.h"

class Player; //the problem seems to be here
class CardException: public BaseCardException
{
public:
    CardException(const Player& p);
};
#endif

using this cardException.h I get the errors:cardException.h: error: expected class-name before ‘{’ token and cardException.h: error: multiple types in one declaration

if I use this for cardException:

#ifndef CARDEXCEPTION_H
#define CARDEXCEPTION_H
#include "baseCardException.h"

class BaseCardException; //this changed
class CardException: public BaseCardException
...

the errors: cardException.h: error: invalid use of incomplete type ‘class BaseCardException’ class CardException: public BaseCardException and CardException.h: error: ‘Player’ does not name a type occurs.

If use both forward declarations: cardException.h:8:7: error: multiple types in one declaration class BaseCardException and cardException.h: error: invalid use of incomplete type ‘class BaseCardException’

I just want to know what am I doing wrong here?

COS
  • 11
  • 3
  • 1
    `class abstractPlayerBase.h;` is a wrong and unnecessary forward declaration. Also, notice that the errors reported by compiler might in fact be a result of wrong code in files being included. – orhtej2 Oct 07 '17 at 23:06
  • `class abstractPlayerBase.h` within the player.h is invalid syntax. That will cause compilation to break for any header file that includes player.h. Rather than reading the LAST error message issued by the compiler, trhy scrolling back and reading the FIRST. The first error message is more likely to represent an actual problem, whereas the last error message may well be garbled due to the compiler being confused by earlier problems. – Peter Oct 07 '17 at 23:10
  • Create a [mcve]. – eerorika Oct 07 '17 at 23:28
  • You have circular includes - `player.h` includes `cardException.h`, which includes `baseCardException.h`, which includes `player.h`. See https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes – Igor Tandetnik Oct 08 '17 at 02:13

1 Answers1

1

BaseCardException.h seems to contain the declaration for a class called CardException, but your naming convention would appear to suggest it should contain a class called BaseCardException.

You're getting the error you're getting because the compiler can't find the definition of the BaseCardException class at the point where the CardException class attempts to inherit from it.

Also, where's the definition of the AbstractPlayerBase class?

Steve
  • 1,747
  • 10
  • 18