hi i have used the template in my simulation program but it generate the error, my program line of code that generate the error is (these code are in queue.template file)

template <class QueueItem>
queue <QueueItem>::~queue() // line25
{

}

template <class QueueItem>
void queue<QueueItem>::push(const QueueItem& entry) //line 42
{

}

template <class QueueItem>
std::ostream& operator << (std::ostream& out_s, const queue<QueueItem>& q)
{
typename queue<QueueItem> ::Node *QueueCursor; // line 85
// code here
}

ERROR generated are
queue.template:25: error: expected constructor, destructor, or type conversion before '<
queue.template:25: error: expected `;' before '<' token
queue.template:42: error: expected init-declarator before '<' token
queue.template:42: error: expected `;' before '<' token

queue.template:84: error: ISO C++ forbids declaration of `queue' with no type
queue.template: In function `std::ostream& CSCI30l_queue::operator<<(std::ostream&, int)
queue.template:85: error: expected nested-name-specifier before "queue"
queue.template:85: error: expected `(' before '<' token
queue.template:85: error: expected primary-expression before '>' token
queue.template:85: error: `::Node' has not been declared
queue.template:85: error: `QueueCursor' undeclared (first use this function)
queue.template:85: error: (Each undeclared identifier is reported only once for each fun )
queue.template:86: error: `q' undeclared (first use this function)

I could not figure out the problem

here the queue is the class that have the constructor and class itself defined in the queue.h file and this queue.template file is the implementation of the member function defined in the queue.h file

Your syntax needs help. See the C++ FAQ Lite.

The difference between a regular class or function and a template class or function is just that you have template<typename Foo> in front of it, and can use Foo as a generic type instead of a specific type like int, or string or somesuch.

So:

//print an array of ints
void printa( int a[], int len ) {
  for (int i = 0; i < len; i++)
    std::cout << a[ i ] << std::endl;
  }

can be made to print any printable thing by turning it into a template function:

//print an array of any printable thing
template<typename PrintableThing>
void printa( PrintableThing a[], int len ) {
  for (int i = 0; i < len; i++)
    std::cout << a[ i ] << std::endl;
  }

Now I can print arrays of strings, floats, etc...

Compare the difference between the two functions, read your textbook over again, and look at the C++ FAQ Lite page I gave you. Also, use the word "typename" instead of "class" in your template declarations.

Hope this helps.

i do not find error in my code, what u are trying to tell is right, i have the same rule follows, i have gone through the michael main, 'data structure and other objects using C++",

This article has been dead for over six months. Start a new discussion instead.