C++ unable to assign objects passed as pointer in methods to variable

265 views Asked by At

I have a class named MyClass declared as follows:

#pragma once

class Object_Class;

class MyClass
{
   static Object_Class *object;

public:

    static void setObject_Class(Object_Class *var);
};

object is a private static variable that points to an object of class Object_Class. In the implementation file, I am trying do this:

void MyClass::setObject_Class(Object_Class *var) {
    object = var;
}

However, I am getting a symbol not found for i386 architecture error pointing to this assignment. What am I doing wrong, and how do I fix this? Is this the best way to pass object and store it in another class variable, or is there a better way?

1

There are 1 answers

5
c-smile On BEST ANSWER

You need to declare and initialize storage for static objects:

Object_Class *MyClass::object = nullptr;

void MyClass::setObject_Class(Object_Class *var) {
    object = var;
}