在 VC++ CLR 项目中,我有一个临时类。我正在尝试将静态变量 temp1 设置为 5。
我遇到编译错误:
Error 32 error LNK2020: unresolved token (0A0005FB) "public: static int temp::temp1" (?temp1@temp@@2HA) C:\Users\user100\Documents\Visual Studio 2012\NewProject 32 bit\create min bars from data2\create min bars from data\create min bars from data5.obj
Error 33 error LNK2001: unresolved external symbol "public: static int temp::temp1" (?temp1@temp@@2HA) C:\Users\user100\Documents\Visual Studio 2012\NewProject 32 bit\create min bars from data2\create min bars from data\create min bars from data5.obj
我该如何解决?
class temp
{
public:
static int temp1;
};
int main(array<System::String ^> ^args)
{
temp::temp1 = 5;
}
最佳答案
在类中声明静态变量时,实际上并没有创建内存。您需要一个单独的变量标注来实际为其创建 RAM。这就是编译器告诉您的内容。
//Outside your class declaration:
int temp::temp1;
关于c++ - 如何为托管 C++ 类中的静态变量赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17353643/