所以我收到以下错误:
..\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_ */
为什么我会收到这些错误,我该怎么做才能修复它们?我假设它与命名空间有关...
答案 0 :(得分:12)
您的头文件具有循环依赖性
Attack.h
包括Actor.h
,反之亦然
使用类的 Forward Declaration 来避免循环依赖性问题。
自OP的评论以来,需要做的是:
class Actor;
class Attack
{
};
如果您的代码在执行此操作后无法编译,则需要阅读链接的答案并了解错误原因以及解决方法。链接的答案解释了这一切。
答案 1 :(得分:0)
类Actor
和Attack
都相互引用,因此您需要在其中一个文件中添加前向声明。
例如,在Actor.h中:
class Attack;
class Actor
{
...
};