0

this is example code that i got the error "undefined reference to`Parent::mMyClass'"

class Parent{ //A and B inherit Parent for mMyClass variable 

protected:
    enum MyClass{
      ClassA,
      ClassB,
    };

    static MyClass mMyClass;
};

class A : Parent {
public:
    void setClass(){
        mMyClass = Parent::ClassA; //Error Here
    }
};
class B : Parent {
public:
    void setClass(){
        mMyClass = Parent::ClassB; //Error Here
    }
};

int main()
{
    A a;
    B b;
    a.setClass();
    b.setClass();

    return 0;
}

I am trying to share the variable "mMyClass" to use for class A and B. Any solution for solving this problem in a better way? Thanks

JustWe
  • 4,250
  • 3
  • 39
  • 90
  • What are you trying to do, this code doesn't make sense, are trying to create a signleton object ? – Captain Wise Oct 23 '15 at 02:24
  • For example : a company have two staffs A and B, they both need to modify the same company document file(a variable). So i make A and B inherit Company and share the document which belong to the Company – JustWe Oct 23 '15 at 03:01
  • Sorry i read your post too fast, i see now that is a enum, it's why i was confused, no your code make sense. Sorry. – Captain Wise Oct 23 '15 at 03:14

1 Answers1

0

A static class member must be defined outside the class. Add the line

Parent::MyClass Parent::mMyClass = Parent::ClassA;

outside the class.

R Sahu
  • 204,454
  • 14
  • 159
  • 270