在下面的代码中,我收到此错误:对象引用未设置为对象的实例
public ref class UtilityFunction
{
UtilityFunction(void)
{
temp = 2;
}
public:
int temp;
public: void runTest()
{
int tmp = this -> temp; //this line gets error
}
};
在另一个类中调用它:
public ref class Startup : public System::Windows::Forms::Form
{
private: void foo()
{
UtilityFunction^ utilityfunc;
utilityfunc->runTest();
}
};
在“int tmp = this - > temp;”行中运行时这是一个未定义的值!
为什么会这样?怎么了?
答案 0 :(得分:1)
UtilityFunction^ utilityfunc = gcnew UtilityFunction();
utilityfunc->runTest();
您需要在使用之前创建类实例。