0

I have an abstract class on which I would like to include as a static variable an array of pointers for that same class. This array would be later used by the derived classes.

class Base {
  public:
    virtual int someValue() = 0;
    static Base* Bases[100];

    void Startup() {
      Bases[2] = this; // just a test
    };
};

class Derived : public Base {
  public:
    virtual int someValue() {return 10};
};

Yet, when compiling, I get the following error: Undefined symbols for architecture i386 "Bases", referenced from: Base::Startup().

How can I achieve this result?

2
  • Members declared static have to be defined outside the class. Commented May 22, 2012 at 22:40
  • You should consider using std::vector. It's a lot less work. Commented May 22, 2012 at 22:43

1 Answer 1

3

You have declared your array Bases, but you have not defined it. You need to define it somewhere else, preferably on a cpp file:

Base* Base::Bases[100];
Sign up to request clarification or add additional context in comments.

1 Comment

That simple? Wow, I can't believe I took so long to figure it out. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.