0

在 VC++ CLR 项目中,我有一个类 temp。我正在尝试将静态变量 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 bar from data2\create min bar from data\create min bar from data5.obj

错误 33 错误 LNK2001: 无法解析的外部符号“public: static int temp::temp1”(?temp1@temp@@2HA) C:\Users\user100\Documents\Visual Studio 2012\NewProject 32 bit\create min bar from data2\从数据创建最小柱线\从 data5.obj 创建最小柱线

我该如何解决?

class temp
{
    public:
    static int temp1;
};

int main(array<System::String ^> ^args)
{
    temp::temp1 = 5;
}
4

2 回答 2

6

在类中声明静态变量时,实际上并没有创建内存。您需要一个单独的变量标注来实际为其创建 RAM。这就是编译器告诉你的。

//Outside your class declaration:
int temp::temp1;
于 2013-06-27T21:39:30.540 回答
5

定义你的静态成员变量:

class temp
{
    public:
        static int temp1;
};

int temp::temp1 = 0;

// Fixed main() ;)
int main(int argc, char** argv)
{
        temp::temp1 = 5;
        return 0;
}
于 2013-06-27T21:38:48.117 回答