我環顧了一會兒,嘗試了很多不同的方法來解決這個問題,但仍然看不到過去的錯誤發生在一個程序中的前向聲明依賴類。正向聲明導致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