c++generic-programmingexplicit-specializationclass-template

"expected initializer before '<' token" attempting template member specialization


I'm trying to define a template member inside a template class.

Here is a fragment of the header file:

template <typename Type>
class Queue
{
private:
// class scope definitions
    // Node is a nested structure definition local to this class
    struct Node {Type item; struct Node* next;};
    enum {Q_SIZE = 10};
    template <typename Type2> class DeepCopy // template member
    {
    public:
        void docopy(Type2& d, const Type2& s);
    };
...

So the template member is defined but I want to make an explicit specialization for the docopy method so it deep-copies when the type is a pointer. I'll put another fragment from the header file with the method template and the specialization:

// template member
template <typename Type>
    template <typename Type2>
void Queue<Type>::DeepCopy<Type2>::docopy(Type2& d, const Type2& s)
{
    d = s;
}

// template member specialization for pointers
template <typename Type>
    template <typename Type2>
void Queue<Type*>::DeepCopy<Type2*>::docopy(Type2* d, const Type2* s)
{
    if (s)
        d = new (*s);
    else
        d = 0;
}

The compiler sends me the following error: expected initializer before '<' token.

I can't figure out what I'm doing wrong. Any help?


Solution

  • You should write your DeepCopy outside Queue as a top level template. Only use it inside your Queue.

       template <typename T>
       class Queue { 
          DeepCopy<T> myCopy;
          ....
              myCopy.doCopy(n.item, newItem);