2010-10-06 59 views
2

所以我基本上有以下代碼。我有工作代碼,並希望將它分成兩個不同的類別,D3DWindowD3DController,而不是全部在D3DWindow。我不相信這是一個問題,因爲它在分離之前就已經開始工作了。問題發生在D3DController.cpp。它說的東西沿着D3DController::Create(D3DWindow*) does not match type D3DController::Create(<error-type>*)的行,所有的文件都在VS2010中,它們都包含在同一個項目中。沒有什麼問題立即成爲我的問題。C++頭問題

stdafx.h中

#include <d3d10.h> 
#include <windows.h> 
#include "D3DWindow.h" 
#include "D3DController.h" 

stdafx.cpp

#include "stdafx.h" 

D3DWindow.h

#include "D3DController.h" 
class D3DWindow{ 
    D3DController controller; 
    public bool init(); 
}; 

D3DWindow.cpp

#include "stdafx.h" 
bool D3DWindow::init(){ 
    if(!controller.create(this)) 
     return false; 
    return true; 
} 

D3DController.h

#include "D3DWindow.h" 
class D3DController{ 
    public bool Create(D3DWindow* window); 
}; 

D3DController.cpp

#include "stdafx.h" 
bool D3DController::Create(D3DWindow* window){ 
    // Do Stuff 
    return true; 
} 
+0

你在D3DWindow.cpp中包含D3DController.h嗎? – fazo 2010-10-06 14:33:58

+0

[在C++中解決循環依賴關係]的可能的重複(http://stackoverflow.com/questions/625799/resolve-circular-dependencies-in-c) – 2010-10-06 14:36:03

回答

2

你有一個循環依賴。也許你可以使用類前向聲明​​而不是#include。例如:

// #include "D3DWindow.h" 

class D3DWindow; // forward declaration 

class D3DController{ 
    public bool Create(D3DWindow* window); 
}; 
+0

很酷,我會給這個鏡頭。前瞻性聲明有沒有細微差別? – Kyle 2010-10-06 14:36:25

+0

從你的代碼片段:D3Dcontroller的頭文件不使用/引用類D3DWindow,但只有一個指向D3DWindow的指針。你可以想到「編譯器不需要知道D3DWindow的字節大小,或者它是這裏的成員」。不要忘記在「D3DController.cpp」中包含「D3DWindow.h」,否則你可能會得到另一個編譯錯誤(「你正在使用未定義的類型」或其他)。沒有告誡。 – 2010-10-06 14:44:33