0

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!

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 2
    You need to read [Why can templates only be implemented in the header file?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – Some programmer dude Feb 12 '17 at 19:03
  • Then you need to read more about how to implement template member functions. Just about any good [beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or Internet tutorial should show it. As a hint: Start to think about why you don't get an error for the *first* function in the source file, only for the rest of the functions. In the source file, what is the difference between the first function in the file, and the rest of the functions? I've taken the liberty to edit the code to show the difference clearer. – Some programmer dude Feb 12 '17 at 19:03
  • 1
    Also, your constructor taking two `, Node next, Node prev)` as the trailing two *value* arguments and saving their *addresses* as members is *not* going to work. – WhozCraig Feb 12 '17 at 19:07
  • Thank you both! I'm new to c++ and we're using the book "Data Structures and Algorithms in C++" Apparently there's an appendix in the back with an example and that made sense with the link to the question that you posted. I added after wherever there was a Node and template above each function and it compiles now! Next I'll fix that constructor –  Feb 12 '17 at 19:47

0 Answers0