// queuetmp.h -- Queue class template (for project Exercise 14.3.cbp)
#ifndef QUEUETMP_H_
#define QUEUETMP_H_
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);
};
// private class members
Node* front; // pointer to front of Queue
Node* rear; // pointer to rear of Queue
int items; // current number of items in Queue
constint qsize; // maximum number of items in Queue
DeepCopy<Type> test; // object member (if the type parameter is a pointer to something, it will generate the corresponding docopy function)
// preemptive definitions to prevent public copying (inline)
Queue(const Queue& q) : qsize(0) { }
Queue& operator=(const Queue& q) { return *this;}
public:
Queue(int qs = Q_SIZE); // create queue with a qs limit (default constructor)
~Queue();
bool isempty() const;
bool isfull() const;
int queuecount() const;
bool enqueue(const Type& item); // add item to end
bool enqueue(const Type* item); // version for pointers (to avoid shallow copying)
bool dequeue(Type& item); // remove item from front
bool dequeue(Type* item);
};
OK. 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 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. (line 12 on the second fragment).
I can't figure out what I'm doing wrong. Any help?
Modify the code -
change void do_copy( B*& dest, const B* srce )
to void do_copy( B* dest, const B* srce )
- and run the program. And you would have your answer.