C++ generic Link list using templates

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

  1. Rhalliwell1 macrumors 6502a

    May 22, 2008
    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 :

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

    Node.cpp :

    #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


    Nov 25, 2005
    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

    Aug 9, 2009
    Sailing beyond the sunset

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

    Jan 6, 2004
    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

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

    Apr 24, 2008
    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


    Dec 16, 2010
    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.

    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

    Aug 9, 2009
    Sailing beyond the sunset
    The thread was started nearly 2 years ago. It was revived by a noob, for no clear reason.
  9. Sander macrumors 6502

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

Share This Page