我在这里搜索了很多次,发现这个问题很多次,但作者通常没有提供代码示例,我今天遇到了这个问题,但我不太确定如何解决它。
1 个错误列表 bool Init(?@@A_NA) 已经在 Client.obj 中定义
。这是我的 Client.cpp、Main.cpp 和 Main.h 的部分代码。
客户端.cpp
#include "stdafx.h"
#include "Main.h"
// the rest of the code doesn't have anything to do with this error..
主要.h
#include "stdafx.h"
bool Init;
// the rest of the code doesn't have anything to do with this error..
main.cpp
#include "stdafx.h"
#include "Main.h"
int main()
{
Init = false;
return 0;
}
最佳答案
在 #include "Main.h"
中,您定义了 bool Init;
,因此每次包含 Main.h
时,您将重新定义 Init
。如果您将 Init
设为静态,
static bool Init;
此处 Init
将具有页面级范围,您的代码应该可以正常工作。
或者更好的是,你在 Math.h
中将 Init
设置为 extern,
extern bool Init;
然后在.cpp文件中定义它,这样你就会有多个声明,但一个定义。
关于c++ - 链接器 LNK2005 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27699548/