I'm trying to make a double linked list of objects in c++. So far I have a Node and DLinkedList class and they worked with integers, but when I tried to convert the type from int to object using the template I'm getting errors. I tried researching but I can't find a good example that uses a header and implementation file with the object template. Can someone help me out?
Here's my header file Node.h:
template <typename Object>
class Node {
public:
Node(const Object &data, Node next, Node prev);
Node(const Object &data);
const Object& getData();
Node* getNext();
Node* getPrev();
void setData(const Object &data);
void setNext(Node *next);
void setPrev(Node *prev);
private:
Object data;
Node *next;
Node *prev;
};
#endif
And this is my Node.cpp file:
template <typename Object>
Node<Object>::Node(const Object &data, Node next, Node prev){
this->data = data;
this->next = &next;
this->prev = &prev;
}
Node<Object>::Node(const Object &data){
this->data = data;
this->prev = nullptr;
this->next = nullptr;
}
const Object& Node<Object>::getData(){
return this->data;
}
Node* Node<Object>::getNext(){
return this->next;
}
Node* Node<Object>::getPrev(){
return this->prev;
}
void Node<Object>::setData(const Object &data){
this->data = data;
}
void Node<Object>::setNext(Node *next){
this->next = next;
}
void Node<Object>::setPrev(Node *prev){
this->prev = prev;
}
These are the first few errors I'm getting:
Node.cpp:24:6: error: use of undeclared identifier 'Object'
Node<Object>::Node(const Object &data){
^
Node.cpp:24:26: error: unknown type name 'Object'
Node<Object>::Node(const Object &data){
^
Node.cpp:24:15: error: extra qualification on member 'Node'
Node<Object>::Node(const Object &data){
~~^
Node.cpp:25:2: error: invalid use of 'this' outside of a non-static member
function
this->data = data;
^
The rest of the errors are similar for the rest of the code. I don't understand why it's not recognizing Object when I defined it in the template. Any advice on how to fix this mess is appreciated!