我环顾了一会儿,尝试了很多不同的方法来解决这个问题,但仍然看不到过去的错误发生在一个程序中的前向声明依赖类。正向声明导致VC++错误,不知道如何解决
这里是我当前的代码的结构的抽象视图,分成6个文件+ 1个主程序文件:
文件XH:
#ifndef X_H
#define X_H
using namespace std;
class y;
class x
{
private:
y *m_oY;
public:
// constructors &c
};
#endif
x.cpp文件:
#include "x.h"
#include "y.h"
// Fancy stuff...
文件yh:
#ifndef Y_H
#define Y_H
using namespace std;
class z;
class y
{
private:
z *m_oZ;
public:
// constructors &c
z *funcZ()
};
#endif
y.cpp文件:
#include "y.h"
#include "z.h"
// Fancy stuff...
文件ZH:
#ifndef Z_H
#define Z_H
using namespace std;
class z { ... };
#endif
文件z.cpp:
#include "z.h"
// Fancy stuff...
文件main.cpp中:
#include "z.h"
#include "y.h"
#include "x.h"
#include <iostream>
using namespace std;
int main() { ... }
的第一个错误我收到了,试图在VS中编译一个干净的非PCH,非尝试使用Class z时,-ATL项目在我的实现中发生。该错误告诉我它使用y.h中z的定义,并且我不确定如何在不创建循环包含问题的情况下解决此问题。该错误的文本如下:
的声明main.cpp中(114):错误C2514: 'Z':类没有构造
YH(9):见 'Z'
任何线索,我在做什么错在这里?
它肯定听起来像main.cpp,它根本没有在'z.h'中看到'z'的定义,并且只能看到来自其他头文件之一的'class z;'声明。您发布的代码对我来说看起来很好,所以我怀疑还有其他一些有趣的事情。攻击这种问题的一种方法是告诉预处理器在没有编译的情况下运行,所以你可以看到所有的#includes是如何实现的。我不记得如何在Visual C++中做到这一点,但也许别人会这么做。 – dfan 2011-03-31 18:32:32
Z有构造函数吗?你有没有尝试从y.h中删除预先声明,包括z.h,因为z似乎不依赖于y? – forsvarir 2011-03-31 18:32:59
因为错误与构造函数有关,所以发布'class z'中完成的代码。还要将所有使用指令放在源文件中(即'使用namespace std;')。从标题中删除它。 – Mahesh 2011-03-31 18:33:53