循环包括和转发声明类
Circular include and forward declaration classes
我真的是c++的新手,我正在尝试完成一个小项目来理解继承。我在包含和转发声明方面遇到问题。以下是以下似乎有问题的标头:
玩家.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
使用此卡异常.h 我得到错误:cardException.h: error: expected class-name before ‘{’ token
和cardException.h: error: multiple types in one declaration
如果我将其用于卡异常:
#ifndef CARDEXCEPTION_H
#define CARDEXCEPTION_H
#include "baseCardException.h"
class BaseCardException; //this changed
class CardException: public BaseCardException
...
错误:发生cardException.h: error: invalid use of incomplete type ‘class BaseCardException’
class CardException: public BaseCardException
和CardException.h: error: ‘Player’ does not name a type
。
如果同时使用前向声明:cardException.h:8:7: error: multiple types in one declaration
class BaseCardException
和 cardException.h: error: invalid use of incomplete type ‘class BaseCardException’
只想知道我在这里做错了什么?
声明,但您的命名约定似乎表明它应该包含一个名为 BaseCardException 的类。
您收到错误是因为编译器在 CardException 类尝试从 BaseException 类继承时找不到该类的定义。
另外,AbstractPlayerBase 类的定义在哪里?
- 如何在 C++ 中转发声明 std::set?
- C++使用默认模板参数键入别名和转发声明
- 如何转发声明枚举?
- 使用函数指针转发声明作为 lamba 声明
- 如何将枚举类转发声明为模板化类的内部类?
- 转发声明在命名空间中不起作用的替代方法
- 如何正确转发声明结构"using XXX"?
- 在类内和类外的定义处执行类转发声明是否有区别
- 为什么转发声明的好友类不能在类中引用?
- 如何访问转发声明类的成员
- 仅在标头开发中转发声明
- 无法转发声明C typedef结构-正在查找其他建议
- boost::hana 转发声明标头的用例是什么?
- 循环包含依赖项/转发声明
- c++ 使用复杂的依赖项获取正确的转发声明
- 如何转发声明依赖于变量定义的类,而变体定义又依赖于模板化类?
- 完美转发使用结构化绑定声明的变量
- 如何声明接受转发引用并返回引用或副本的函数模板
- MSVC 在转发类模板声明中阻塞
- 在 cpp 文件中声明转发的类定义,干什么