rwols
rwols

Reputation: 3078

C++ template class specializations and structs

I've spent hours searching on the web for a solution but to no avail. I'm programming C++ in Xcode

#import "data.h" // contains a struct called data

template <class T>
class container {
    public:
        container();
        ~container();
    private:
        // functionality for containing T
};

template <class T>
container<T>::container() { /* generic */ }

template <class T>
container<T>::~container() { /* generic */ }

template <>
container<data>::container() { /* template specialization of data */ }

The compiler complains: duplicate symbol and points out the class template specialization. I thought that maybe it was because of constructs not being able to specialize, so I tried something along the lines of adding an extra void function

template <class T>
class container {
    public:
        container();
        ~container();
        void setup();
    private:
        // functionality for containing T
};

template <>
void container<data>::setup() { /* template specialization of data */ }

But this gives me the same compiler error. I don't really have any idea of where to look for a solution now...

Upvotes: 1

Views: 1144

Answers (1)

vdsf
vdsf

Reputation: 1618

When you specialize a class template, you must specialize ALL of the member functions.

In addition to setup, you still need to specialize the constructor/destructor.

template <>
container<data>::container()
{
  // ...
}

template <>
container<data>::~container()
{
  // ...
}

Upvotes: 1

Related Questions