静态函数错误LNK2001:未解析的外部符号"private: static int B::s_nValue"
static function error LNK2001: unresolved external symbol "private: static int B::s_nValue"
#pragma once
class B
{
private:
static int s_nValue;
public:
B();
~B();
static int GetValue() { return s_nValue; }
static void SetValue(int value){ s_nValue = value; }
};
int _tmain(int argc, _TCHAR* argv[])
{
int i = 0;
B::SetValue(5);
std::cout << B::GetValue();
cin>> i;
return 0;
}
我正在尝试掌握静态函数的使用。在本例中,SetValue(( 和 GetValue(( 在类 B 中定义。 在 main 中调用它们时没有对象定义。这应该有效,但我收到一个未解决的外部符号错误 2001。预编译标头选项处于关闭状态。
如何摆脱此错误?
使用 MS Visual Studio 2012
静态
变量只声明而未定义,您应该在 cpp 文件中执行此操作:
int B::s_nValue = 0;
相关文章:
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 如何处理 c++ 中类实现中的"invalid use of non-static data member"?
- std::make_shared和protected/private构造函数
- 收到错误"invalid use of non-static data member 'stu::n' "
- LNK1104:无法打开libpjproject-i386-Win32-vc14-Debug-Static.lib
- 我应该在 C++ 中何时/为什么使用 STATIC?
- 在VS2019项目中集成ImageMagick:x64-windows-static library
- 如何处理Boost Spirit X3导致Visual Studio 2019 "static initialization order fiasco"?
- "static char __ = []() -> char"的含义
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- 无法在 DLL 中链接 SDL2-static.lib
- 如何摆脱C++中未解析的外部符号"private: static char"错误?
- C++ - "private"单身人士?
- C++线程"Call to non-static member function without an object argument"
- 出现这种错误的原因是什么"invalid use of non-static data member "
- 将复制构造函数设置为private和=delete有什么区别
- 错误 LNK2001:未解析的外部符号"private: static class Game Game::game_"
- 未解析的外部符号"private: static int Math::result"
- 静态函数错误LNK2001:未解析的外部符号"private: static int B::s_nValue"
- Player.obj :错误LNK2001:未解析的外部符号"private: static class InputHandler * InputHandler::s_pInstance"