samedi 7 octobre 2017

Circular include and forward declaration classes

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.h;
class Player: public AbstractPlayerBase
{
   ...
   //a function throws a CardException
};
#endif

baseCardException.h:

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

class CardException
{
...
};
#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?

Aucun commentaire:

Enregistrer un commentaire