c++ - 如何为托管 C++ 类中的静态变量赋值?

标签 c++ visual-c++ static managed-c++

在 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/

相关文章:

c++ - 我可以取消单例一个单例吗

c++ - opencv相机标定(Assertion failed故障)

c++ - 如何使用正则表达式匹配仅包含中文字母的字符串?

visual-studio - 为什么 Visual Studio 2010 无法找到/打开 PDB 文件?

static - 如何链接目标文件 (*.o) 和静态库文件 (*.a)

C++ 静态存储持续时间对象在 main() 之前初始化

java - 静态 fragment 与非静态 fragment

c++ - void(int) 和 void(*)(int) 有什么区别?

c++ - 使用输入和输出运算符读取用户定义类型的对象

c++ - static_cast<std::u16string> 的 GCC 问题