下面我重现了编译时出现错误的部分代码的简化版本。这里有循环依赖吗?
testing.cpp
#include <iostream>
#include "../Beta.h"
#include "../Alpha.h"
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
Alpha.h
#include <vector>
class Alpha
{
public:
typedef struct _info{
int k;
} info;
friend class Beta;
};
Beta.h
#include <vector>
class Alpha;
class Beta
{
public:
std::vector <Alpha::info*> vecInfo;
};
当我运行g++ testing.cpp
我收到以下错误消息
In file included from testing.cpp:10:0: ../Beta.h:8:15: error:
incomplete type ‘Alpha’ used in nested name specifier ../Beta.h:8:15:
error: incomplete type ‘Alpha’ used in nested name specifier
../Beta.h:8:27: error: template argument 1 is invalid ../Beta.h:8:27:
error: template argument 2 is invalid
我可以从论坛主题的第一个错误是因为某种循环依赖(Error: incomplete type used in nested name specifier)的见。我无法看到我的代码和他们的代码之间的相似性。我究竟做错了什么。
这不是一个循环依赖。它只是意味着你不能引用'Alpha :: info *',因为它没有在任何地方声明过。 – us2012 2013-02-15 17:44:38