-
Templates
Hello! I have a question that has to do with templates...
I have the following template:
Code:
template <class T> class list
{
public:
struct Node
{... ... ...};
...
...
Node* move_back(Node* x)
{
return x->prev;
}
private:
Node* head;
};
I have written this template in .h file and I want to move it to .cpp source file. My problem is that I don't know how to write the function in the .cpp file (it's just a copy-paste but how should I write the prototype of the function move_back?)
Any kind of help will be very useful for me...
-
Code:
template <class T> Node* list<T>::move_back(Node* x)
{
// ...
}
?
I tend to write most of the function bodies inside the class declaration if I can. Typing template <class T>... for every member function quickly gets annoying.
-
Nope... :(
Code:
template <class T>
Node* list<T>::move_back(Node* x)
{
return x->prev;
}
4 errors at line 2:
syntax error : missing ';' before '*'
'Node' : missing storage-class or type specifiers
syntax error : ';'
'T' : undeclared identifier
I wanted to write it inline (and it's the best solution) but in this case I have to write it in the .cpp file.
-
Node is a nested class inside list, so you have to qualify it as list<T>::Node.
>> I wanted to write it inline (and it's the best solution) but in this case I have to write it in the .cpp file.
Remember that unless you include the cpp file in your header file, or use some other solution (see http://www.parashift.com/c++-faq-lit...html#faq-35.15) you will get linker errors when you put the function definitions in the cpp file.
-
>> Node is a nested class inside list, so you have to qualify it as list<T>::Node.
Damn I didn't notice that. I should've tried to compile it myself before replying :(
*learnt lesson*