为什么错误LNK2001:在这种情况下未解析的外部符号
why error LNK2001: unresolved external symbol in this case?
可能的重复项:
在C++
中定义静态成员 具有字段的静态方法
我在网络上找到了以下关于单例实现的代码,并决定尝试一下:
#include <iostream>
class Singleton
{
Singleton(){}
static Singleton *s_instance;
public:
static Singleton* getInstance()
{
if(!s_instance)
s_instance = new Singleton();
return s_instance;
}
};
int main()
{
Singleton::getInstance();
return(0);
}
它看起来很简单。但是当我在Visual Studio中构建它时,它给出了一个链接器错误消息:
main.obj : error LNK2001: unresolved external symbol "private: static class Singleton
* Singleton::s_instance" (?s_instance@Singleton@@0PAV1@A)
C:UsersbollDocumentsVisual Studio 2010Projectshello_worldDebughello_world.exe :
fatal error LNK1120: 1 unresolved externals'
为什么在这种情况下"s_instance
"没有解决?
我认为你应该先初始化s_instance=NULL
。您可以看到以下链接:http://www.codeproject.com/Articles/1921/Singleton-Pattern-its-implementation-with-C
相关文章:
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 未解析的外部符号_MsiLocateComponentW@12.
- 在C++中使用 gRPC 时未解析的外部符号
- LibPrivoxy: 未解析的外部符号 __declspec(dllimport) int __stdcall Sta
- 尝试从 XamlApplication 编译 C++/WinRT 空白应用程序时,我收到未解析的外部符号 winrt_make_*
- 我收到错误LNK2001:未解析的外部符号(C++代码)
- 可视抽象类 c++(错误 LNK 2001:未解析的外部符号)
- 如何摆脱C++中未解析的外部符号"private: static char"错误?
- 当 lib 已添加到其他依赖项时,如何在 VS 中调试未解析的外部符号错误
- 仅在少数方法(静态或共享库)中解析的外部符号
- 未解析的外部符号"__declspec(dllimport)与 Spdlog c++ 库
- 函数 _main (OPENGL C++) 中引用的未解析的外部符号 ________
- 错误LNK2019未解析的外部符号"public: __thiscall SLinkList<char>::SLinkList<char>(void)"
- 使用静态 constexpr 成员的未解析外部符号
- 引入参数化构造函数后显示 LNK 2019 未解析外部符号的代码错误
- Dlib LNK2001未解析的外部符号USER_ERROR__consistent_build_configurati
- 矛盾的未解决的外部符号+未使用的库与VS2017和FFMPEG 4
- 链接器错误:切换到unicode生成给出:未解析的外部符号WinMain
- 类模板上一个特定函数的未解析外部符号
- LNK2001:未解析的外部符号public:static类std::vector