user3001145
user3001145

Reputation: 11

object reference not set to an instance of an object (C++/CLI)

In the code below i get this error: object reference not set to an instance of an object

public ref class UtilityFunction
{
    UtilityFunction(void)
    {
        temp = 2;
    }

    public:
    int temp;

    public: void runTest()
    {
        int tmp = this -> temp;   //this line gets error
    }
};

it is called within another class:

public ref class Startup : public System::Windows::Forms::Form
{

private: void foo()
    {
        UtilityFunction^ utilityfunc;
        utilityfunc->runTest();
    }
};

when running in the line "int tmp = this -> temp;" this is an undefined value!

why is that so? whats wrong?

Upvotes: 0

Views: 6095

Answers (1)

Alex F
Alex F

Reputation: 43311

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

You need to create class instance before using it.

Upvotes: 2

Related Questions