0

在下面的代码中,我收到此错误:对象引用未设置为对象的实例

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;”行中运行时 这是一个未定义的值!

为什么呢?怎么了?

4

1 回答 1

2
UtilityFunction^ utilityfunc = gcnew UtilityFunction();
utilityfunc->runTest();

您需要在使用它之前创建类实例。

于 2013-11-17T08:23:42.563 回答