C++ C++;标题问题
所以我基本上有以下代码。我有工作代码,希望将其中的一些代码分为两个不同的类,C++ C++;标题问题,c++,visual-studio-2010,C++,Visual Studio 2010,所以我基本上有以下代码。我有工作代码,希望将其中的一些代码分为两个不同的类,D3DWindow和D3DController,而不是全部放在D3DWindow中。我不认为这是pch的问题,因为它在分离之前就已经工作了。问题出现在D3DController.cpp中。它说,类似于D3DController::Create(D3DWindow*)的内容与D3DController::Create(*)类型不匹配。所有文件都在VS2010中,并且都包含在同一个项目中。对我来说,没有什么比这个问题更突出的
D3DWindow
和D3DController
,而不是全部放在D3DWindow
中。我不认为这是pch的问题,因为它在分离之前就已经工作了。问题出现在D3DController.cpp中。它说,类似于D3DController::Create(D3DWindow*)的内容与D3DController::Create(*)类型不匹配。所有文件都在VS2010中,并且都包含在同一个项目中。对我来说,没有什么比这个问题更突出的了
stdafx.h
#include <d3d10.h>
#include <windows.h>
#include "D3DWindow.h"
#include "D3DController.h"
#include "D3DController.h"
class D3DWindow{
D3DController controller;
public bool init();
};
#include "D3DWindow.h"
class D3DController{
public bool Create(D3DWindow* window);
};
D3DWindow.h
#include <d3d10.h>
#include <windows.h>
#include "D3DWindow.h"
#include "D3DController.h"
#include "D3DController.h"
class D3DWindow{
D3DController controller;
public bool init();
};
#include "D3DWindow.h"
class D3DController{
public bool Create(D3DWindow* window);
};
D3DWindow.cpp
#include "stdafx.h"
#include "stdafx.h"
bool D3DWindow::init(){
if(!controller.create(this))
return false;
return true;
}
#include "stdafx.h"
bool D3DController::Create(D3DWindow* window){
// Do Stuff
return true;
}
D3DController.h
#include <d3d10.h>
#include <windows.h>
#include "D3DWindow.h"
#include "D3DController.h"
#include "D3DController.h"
class D3DWindow{
D3DController controller;
public bool init();
};
#include "D3DWindow.h"
class D3DController{
public bool Create(D3DWindow* window);
};
D3DController.cpp
#include "stdafx.h"
#include "stdafx.h"
bool D3DWindow::init(){
if(!controller.create(this))
return false;
return true;
}
#include "stdafx.h"
bool D3DController::Create(D3DWindow* window){
// Do Stuff
return true;
}
你有一个循环依赖。也许您可以使用类转发声明而不是
#include
。例如:
// #include "D3DWindow.h"
class D3DWindow; // forward declaration
class D3DController{
public bool Create(D3DWindow* window);
};
你是否在D3DWindow.cpp中包含D3DController.h?可能是Cool的复制品,我来试一试。转发声明有什么细微差别吗?从您的代码片段来看:D3Dcontroller的头文件不使用/引用类D3DWindow,而只使用指向D3DWindow的指针。您可以想象“编译器不必知道D3DWindow的字节大小或它的成员”。别忘了在“D3DController.cpp”中包含“D3DWindow.h”,否则可能会出现另一个编译错误(“您使用的是未定义的类型”或其他内容)。没有警告。