I am trying to learn templates usage in c++. I have created a struct node which I am using in queue class implementation but I am getting compiler error: Error" expected type specifier before qnode in member function bool MyQueue::add(T data)
#include <iostream>
using namespace std;
template <typename T>
struct qnode {
qnode* Node;
T data;
};
template <class T>
class MyQueue {
qnode<T>* front;
qnode<T>* end;
public:
MyQueue() {
front=NULL;
end=NULL;
}
bool add (T n);
T get(void);
bool empty(void)
{
if ( front == NULL)
return true;
else
return false;
}
size_t size(void)
{
}
};
template <typename T>
bool MyQueue<T>::add ( T n)
{
qnode<T>* temp = new qnode;
temp->data = n;
temp->Node = NULL;
if ( front == NULL )
{
cout << "Adding front qnode " << endl;
front = end= temp;
// front->Node = end;
return true;
}
cout << "Adding qnode " << endl;
end->Node = temp;
end=temp;
//delete temp;
return true;
}
I am looking forward for a nice explanation for how template parameters get resolved in such nested implementation.
Your new
has a syntax error.
qnode<T>* temp = new qnode;
should be
qnode<T>* temp = new qnode<T>();
Remember, a template class without template parameters is meaningless to the compiler. Whenever you type qnode (after the initial declaration), you need to type its template parameters as well!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.