【发布时间】:2015-03-22 19:25:21
【问题描述】:
抱歉,如果您之前看到过这个问题,但它尚未得到回答,基本上在我的代码中,我有两个结构,在单独的标题中定义并在整个项目中全局使用。我只是希望在其他 cpp 文件中使用这两个结构(同样,在两个单独的头文件中定义),而不仅仅是头文件所属的那些。 这是我测试过的一些示例代码:
class1.h
#include "class2.h"
#include <vector>
#include <string>
struct trans1{
string name;
};
class class1 {
private:
vector <trans2> t2;
public:
class1();
};
class2.h
#include "class1.h"
#include <vector>
#include <string>
struct trans2{
string type;
};
class class2{
private:
vector <trans1> t1;
public:
class2();
};
错误日志:
In file included from class1.h:3:0,
from class1.cpp:1:
class2.h:21:13: error: 'trans1' was not declared in this scope
vector <trans1> t1;
^
class2.h:21:19: error: template argument 1 is invalid
vector <trans1> t1;
^
class2.h:21:19: error: template argument 2 is invalid
我知道这在现实世界的应用程序中是荒谬的代码,但这是我可以演示的最简单的方法。
值得注意的是,如果我只是在“private:”下注释掉向量 t1 或 t2 的声明,则代码编译不会失败。这只是我使用第二个结构的事实。
对任何人有帮助吗?谢谢。
【问题讨论】:
-
你能把“trans”结构放在他们自己的头文件中吗?