1

I have class A with method display(). I create a struct B with a static variable of type object A:

class A{
public :
    void display()
    {
        cout << "in A " << endl;
    }
};
typedef struct B{
    static A a;
} bb;

//B::a.display();
int main() {
    bb b;
    bb::a.display();
    return 0;
}

Now I get an error when try to access a.

How can I define a static object in this case?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

4

You declared static A a; but did not define it. Add the following line before int main() and it'll link successfully:

A B::a;
Paul
  • 13,042
  • 3
  • 41
  • 59