Question

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?

Was it helpful?

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);

OTHER TIPS

For your example you should

  1. Partial specialize Queue class for T*.
  2. In partial specialization of Queue<T*> write definition of struct DeepCopy.
  3. Write specialization of Queue<T*>::DeepCopy for U*.
  4. Write function Queue<T*>::DeepCopy<U*>::docopy.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top