C++ generic Link list using templates

Discussion in 'Mac Programming' started by Rhalliwell1, Nov 24, 2009.

  1. Rhalliwell1 macrumors 6502a

    Joined:
    May 22, 2008
    #1
    I am having a problem writing the nodes for the list. I want to return the pointer to the next node but i am getting errors

    Node.h :

    Code:
    #ifndef Node_H
    #define Node_H
    
    template <typename T>
    class Node {
    private:
    	Node* ptr; //points to the next node;
    	T* obj; //value to be stored within the node;
    
    public:
    	//constructors
    	Node(T* theObj, Node* ptr);
    	//copy constructor
    	Node(Node& n);
    
    	T* getNext();
    	T* getObj();	
    };
    #endif

    Node.cpp :

    Code:
    #include "Node.h"
    
    template<typename T>
    Node<T>::Node(T* theObj, Node* thePtr){
    	obj = theObj;
    	ptr = thePtr;
    }
    
    template<typename T>
    Node<T>::Node(Node& n) {
    	ptr = n.getNext;
    	obj = n.getObj;
    }
    
    template<typename T>
    T* Node<T>::getObj() {
    	return obj;
    }
    
    Node* Node::getNext() {
    	return ptr;
    }
    error is at line 20 -------> Node* Node::getNext() {
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    Slightly hard to find the problem, because you didn't say what kind of error you get (really helpful information), but when I read through your header file, there was one method where I thought: "Huh? That can't be right" and if you read the error message carefully, you will probably find what's wrong.

    By the way, do you really want these constructors to be public?
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    Mismatch?

    I think you can find examples with google keywords: c++ linked list template
     
  4. darkwing macrumors 65816

    Joined:
    Jan 6, 2004
    #4
    chown is right. You have a mismatch there. Also, usually with templated classes you'll want to put the code in the .h file either inline or below the class def. Otherwise if you try to include node.h from another file, then it won't work. Or, you'd have to include node.cpp as well.

    Good luck.
     
  5. f.abdorazagh macrumors newbie

    Joined:
    Oct 7, 2011
    #5
    Is writing "node * ptr" true? we should have written :Node<T>*ptr; ,shouldn't we?
    :confused:
     
  6. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #6
    While we're criticizing the design anyway, I'd ask why you are storing T* instead of T.
     
  7. jiminaus, Oct 7, 2011
    Last edited: Oct 7, 2011

    jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #7
    I would have thought all references to Node would have to be Node<T> including in obj's declaration, the constructor parameters, and getNext()'s return type.

    Code:
    template<typename T>
    Node<T>* Node<T>::getNext() {
    	return ptr;
    }
    

    One possibility is that this is a linked list providing a view over another data structure, so obj points into the other data structure. This would explain the lack of destructor deallocating obj. But I think that unlikely. I think it's more likely to be two mistakes.


    But I have a greater question. Why is the OP trying to implement his own linked list instead of using the Standard Template Library?
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    The thread was started nearly 2 years ago. It was revived by a noob, for no clear reason.
     
  9. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #9
    I assumed it was for education. I didn't notice the thread-necro either, so I'll stop posting in here.
     

Share This Page