错误 LNK2001:未解析的外部符号 Visual C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12391253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:14:35  来源:igfitidea点击:

error LNK2001: unresolved external symbol Visual C++

c++visual-studio-2010mfc

提问by Nabeel

Possible Duplicate:
static, extern, constin header file

可能重复:
static, extern,const在头文件中

I am working in Visual C++ and having this error.

我正在使用 Visual C++ 并遇到此错误。

I have declared below two extern lines in global.h seperately.

我在 global.h 中分别声明了以下两条 extern 行。

extern CSocketManager  m_SocketManager[10];
extern CSocketManager* m_pCurServer; 
1>ServerSocketDlg.obj : error LNK2001: unresolved external symbol "class CSocketManager *                      m_pCurServer" (?m_pCurServer@@3PAVCSocketManager@@A)

1>SocketManager.obj : error LNK2001: unresolved external symbol "class CSocketManager * m_pCurServer" (?m_pCurServer@@3PAVCSocketManager@@A)

Does anyone have an idea what might cause these errors?

有谁知道什么可能导致这些错误?

回答by Nikopol

Objects declared in the .h as extern also have to be declared in a .cpp file.

在 .h 中声明为 extern 的对象也必须在 .cpp 文件中声明。

The problem is that linker doesn't know where to find the two objects.

问题是链接器不知道在哪里可以找到这两个对象。

Solution: you also have to declare m_pCurServer in the .cpp file because it's the .cpp that is being compiled, not the header.

解决方案:您还必须在 .cpp 文件中声明 m_pCurServer,因为正在编译的是 .cpp,而不是头文件。