0

I am trying to implement a singly linked list that stores multiple types of item. So I came across templates, but when I tried running the following code, the compiler gives me several linking errors (LNK 2019: unresolved external symbol). I haven't really done anything yet and can't figure out what went wrong. Can anyone please point out my mistake??

singlylinkedlist.h

template <class Item>
class SinglyLinkedList
{
public:
    SinglyLinkedList();
    ~SinglyLinkedList();

private:
    template <class I>
    struct Node {
        I item;
        Node<I> *next;
    };
    Node<Item> *head; 
};

singlylinkedlist.cpp

#include "singlylinkedlist.h"

template <class Item>
SinglyLinkedList<Item>::SinglyLinkedList()
{
    head = NULL;
}

main.cpp

#include <iostream>
#include "singlylinkedlist.h"

using namespace std;

int main()
{
    SinglyLinkedList<string> list;
}
QQO
  • 23
  • 1
  • 5
  • You don't need an extra template parameter for the `Node` struct, it's enough to just use the `Item` parameter from the outer class. – πάντα ῥεῖ Dec 28 '13 at 08:13
  • Thanks for your reply πάντα ῥεῖ and Mat. I actually found that my problem was because I need to say declare SinglyLinkedList *list as pointer in main(). I'm not really sure why though.. – QQO Dec 28 '13 at 08:21
  • Thanks for you comment too, juanchopanza – QQO Dec 28 '13 at 08:30
  • @QQO That doesn't really solve your problem!! Try to initialize this pointer, and you'll have the same compilation errors again. – πάντα ῥεῖ Dec 28 '13 at 08:31
  • You're right, πάντα ῥεῖ, it doesn't work. Thanks for pointing that out for me. – QQO Dec 28 '13 at 08:45
  • Un-template your `Node` and have `next` be a pointer to an `Item` class. – Blender Dec 28 '13 at 09:01

2 Answers2

1

There are a number of small issues with the code, for example, you haven't implemented the destructor, and you don't really need to templatize Node. Change your implementation as follows,

// singlylinkedlist.h
template <class Item>
class SinglyLinkedList
{
public:
    SinglyLinkedList() : head(NULL) {}
    ~SinglyLinkedList() {}

private:
    struct Node {
        Item item;
        Node *next;
    };
    Node *head; 
};

// main.cpp
#include "singlylinkedlist.h"
using namespace std;

int main()
{
    SinglyLinkedList<string> list;
}
mockinterface
  • 14,452
  • 5
  • 28
  • 49
  • I found that my problem was with pointers in which I need to declare list as pointer in main(). Thanks for answering my question. – QQO Dec 28 '13 at 08:27
  • @QQO That doesn't really solve your problem!! Try to initialize this pointer, and you'll have the same compilation errors again. – πάντα ῥεῖ Dec 28 '13 at 08:32
  • I am afraid that you've just deferred the problem. Do what πάντα ῥεῖ said and add `list = new SinglyLinkedList();` – mockinterface Dec 28 '13 at 08:33
  • You guys are right, it doesn't work. I was reading the others' comment and testing mockinterface's code. I can only seem to define template inside header files. – QQO Dec 28 '13 at 08:52
  • @QQO _'I can only seem to define template inside header files.'_ Yes! The compiler needs to see the complete template class definition, when it's used somewhere else. – πάντα ῥεῖ Dec 28 '13 at 08:55
  • Thanks for confirming this line for me, πάντα ῥεῖ. It sounds weird to implement the functions in header files and that's why I had a little doubt. Thanks again, πάντα ῥεῖ and mockinterface! I'll accept this answer. – QQO Dec 28 '13 at 09:07
0

You didn't implement the destructor:

template <class Item>
SinglyLinkedList<Item>::~SinglyLinkedList()
{
    //cleanup
}
egur
  • 7,830
  • 2
  • 27
  • 47