编译我的解决方案时,我遇到了LNK2005错误。它说
"public: class Vector3 const__cdecl Vector3::operator+=(double const &)" (??_0Vector3@@QEAA?BV0@AEBN@Z) already defined in Game.obg file: GameObject.obj
"public: class Vector3 const__cdecl Vector3::operator+=(double const &)" (??_0Vector3@@QEAA?BV0@AEBN@Z) already defined in Game.obg file: Main.obj
"public: class Vector3 const__cdecl Vector3::operator+=(double const &)" (??_0Vector3@@QEAA?BV0@AEBN@Z) already defined in Game.obg file: MainWindow.obj
"public: class Vector3 const__cdecl Vector3::operator+=(double const &)" (??_0Vector3@@QEAA?BV0@AEBN@Z) already defined in Game.obg file: Transform.obj
"public: class Vector3 const__cdecl Vector3::operator+=(double const &)" (??_0Vector3@@QEAA?BV0@AEBN@Z) already defined in Game.obg file: Vector3.obj
我的解决方案组织如下:
Game.h包含GameObject.h和其他东西
GameObject.h包含Transform.h和Component.h
Component.h包含所有组件头文件,例如Rigidbody.h
Transform.h包含Vector3.h
Vector3.h包含cmath
在Game.h中有这样的:
#include "GameObject.h"
// other includes
class Game
{
public:
Game( class MainWindow& wnd );
// other things
private:
MainWindow& wnd;
// other things
}
// other things again
所以问题是:我做错了什么? PS:我试图评论Game.cpp中的所有代码,看看会发生什么,但没有成功
编辑:我已经通过将Vector3.h中的“Vector3 :: operator + =”定义移动到Vector3.cpp来解决它...但不知道为什么它现在有效,有谁知道? (Vector3.h没有包含后卫,但它有#pragma一次,Vector3.cpp既没有包含后卫也没有#pragma一次)