名称空间问题
Namespace problems
所以我得到以下错误:
..Actor.h:35: error: `Attack' is not a member of `RadiantFlux'
..Actor.h:35: error: template argument 1 is invalid
..Actor.h:35: error: template argument 2 is invalid
..Actor.h:35: error: ISO C++ forbids declaration of `attacks' with no type
这一行(除其他行外):
std::vector<RadiantFlux::Attack> attacks;
相关文件如下:
Actor.h:
#ifndef ACTOR_H_
#define ACTOR_H_
#include <string>
#include <vector>
#include "Attack.h"
namespace RadiantFlux {
...
class Actor {
private:
std::string name;
int health;
std::vector<RadiantFlux::Attack> attacks;
Attributes attributes;
public:
...
};
}
#endif /* ACTOR_H_ */
Attack.h:
#ifndef ATTACK_H_
#define ATTACK_H_
#include <string>
#include <stdlib.h>
#include <time.h>
#include "Actor.h"
namespace RadiantFlux {
...
class Attack {
private:
...
public:
...
};
}
#endif /* ATTACK_H_ */
为什么我得到这些错误,我能做些什么来修复它们?我认为这与名称空间有关。
你的头文件有一个循环依赖。Attack.h
包含Actor.h
,反之亦然。
使用类的前向声明来避免循环依赖问题。
鉴于OP的评论,以下是需要做的:
class Actor;
class Attack
{
};
如果你的代码编译失败后,你需要阅读链接的答案和理解为什么错误和如何解决它。链接的答案解释了这一切。
类Actor
和Attack
都相互引用,因此您需要在其中一个文件中添加前向声明。
例如,在Actor.h中:
class Attack;
class Actor
{
...
};
相关文章:
- 如何在不使用额外空间的情况下解决这个问题?
- 有关命名空间和模板参数的名称的问题
- 命名空间范围问题
- 在 c++ 中定义命名空间中模板类的友元函数时出现问题
- 从命名空间嵌入资源的问题(C++和CMake)
- 与自定义命名空间一起使用时实现 std::error_category、名称解析问题
- OPENCV在C /名称空间问题上
- 涉及STD :: __ CXX11名称空间的链接器错误 - 双ABI问题
- 关于"使用命名空间标准"的问题?
- 更改命名空间以自定义 Boost XML 的标记名称后的反序列化问题
- 删除标点符号和空间的问题
- 在将平截头体从摄影机空间转换为灯光空间以进行阴影映射方面存在主要问题
- 有关通过派生类在命名空间内继承的问题
- C++相同的命名空间问题
- 是详细命名空间中的使用指导性的问题
- 命名空间内类的循环依赖问题
- 命名空间外部变量的未定义参考/多重定义,C 结构问题
- C++11 中的命名空间问题?
- 关于取消引用和地址空间的基本C++指针问题
- C++中的编译错误可能是命名空间问题