0

I'm attempting to compile the following:

template<typename T>
class Foo {
protected:
   static Foo<T>* pool[5];
public:
   static Foo* Bar() {
      pool[1] = new Foo();
   }
};

int main() {
   Foo<int>* b = new Foo<int>();
   b->Bar();
}

I get the error:

 undefined reference to `Foo<int>::pool'

How to I cause the pool array to be defined?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Nathaniel Flath
  • 15,477
  • 19
  • 69
  • 94

1 Answers1

2

You can define a static member of a templated class outside of the class like this.

// for generic T
template<typename T>
Foo<T>* Foo<T>::pool[5] = {0, 0, 0, 0, 0};

// specifically for int
template<>
Foo<int>* Foo<int>::pool[5] = {0, 0, 0, 0, 0};
Robert Prévost
  • 1,667
  • 16
  • 22