Template class LinkedList Problem
Apr 17, 2011 at 5:47pm UTC
I'm Getting this error when i compile my Program.
Error 2 error LNK2019: unresolved external symbol "public: void __thiscall SinglyLL<int>::remove(void)" (?remove@?$SinglyLL@H@@QAEXXZ) referenced in function _main
[b] Here is the Code..
.[/b]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
#include<iostream>
using namespace std;
template <class N>
struct Node
{
N data;
Node *next;
Node(){data = 0; next = NULL;}
//Node(T &content){data = content;}
~Node(){delete next;}
};
template <class T>
class SinglyLL
{
struct Node
{
T data;
Node *next;
Node(){data = 0; next = NULL;}
Node(const T &content){data = content;}
~Node(){delete next;}
};
Node* head;
Node* c;
// head holds the complete List
// c Holds the Current Node
public :
SinglyLL();
void add(T n);
void remove();
void next();
void back();
T current();
void display();
~SinglyLL();
};
template <class T>
SinglyLL<T>::SinglyLL()
{
head = NULL;
c = NULL;
}
template <class T>
void SinglyLL<T>::add(T n)
{
Node<n> *temp = new Node();
temp->data = n;
if (head == NULL)
{
// Add the Very First Node, Creation of List
temp->next = NULL;
head = temp;
c = head;
}
else if (c->next == NULL)
{
//Add New Node At The End
c->next = temp;
c = c->next;
}
else
{
// Add New Node After Current Node
Node<n> *temp2 = c->next;
temp->next = temp2;
c->next = temp;
c = c->next;
}
}
template <class T>
void SinglyLL<T>::remove()
{
Node<T> *temp = head;
if (head != c)
{
while (temp->next != c)
{
temp = temp->next;
}
Node<T> *rem = c;
Node<T> *temp2 = c->next;
temp->next = temp2;
temp = c;
delete rem;
}
}
template <class T>
void SinglyLL<T>::next()
{
if (c->next != NULL)
c = c->next;
}
template <class T>
void SinglyLL<T>::back()
{
Node<T> *temp = head;
if (head != c)
{
while (temp->next != c)
{
temp = temp->next;
}
}
c = temp;
}
template <class T>
T SinglyLL<T>::current()
{
return c->data;
}
template <class T>
void SinglyLL<T>::display()
{
Node<T> *temp = head;
while (temp != NULL)
{
cout<<temp->data<<" " ;
temp = temp->next;
}
}
template <class T>
SinglyLL<T>::~SinglyLL()
{
delete head;
delete c;
}
int main()
{
SinglyLL<int > ll;
ll.add(5);
ll.add(10);
ll.add(13);
ll.add(186);
ll.add(132);
ll.back();
ll.add(23);
ll.add(25);
ll.add(444);
//ll.add(23);
ll.display();
cout<<ll.current();
cout<<endl;
ll.remove();
ll.display();
system("pause" );
return 0;
}
Last edited on Apr 17, 2011 at 5:48pm UTC
Apr 17, 2011 at 5:57pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
template <class N>
struct Node
{
N data;
Node *next;
Node(){data = 0; next = NULL;}
//Node(T &content){data = content;}
~Node(){delete next;}
};
template <class T>
class SinglyLL
{
struct Node //not a template
{
T data;
Node *next;
Node(){data = 0; next = NULL;}
Node(const T &content){data = content;}
~Node(){delete next;}
};
//...
template <class T>
void SinglyLL<T>::add(T n)
{
Node<n> *temp = new Node(); //what?
which Node will call (outer or inner)?
why do you pass from a no-template Node to a templatized Node?
n does not name a type (incorrect template parameter)
Apr 18, 2011 at 5:09am UTC
Which compiler are you using? I don't think GNU can complie this through and then give you that error report.
Last edited on Apr 18, 2011 at 5:13am UTC
Apr 18, 2011 at 9:59am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
template <class N>
struct Node
{
N data;
Node *next;
Node(){data = 0; next = NULL;}
//Node(T &content){data = content;}
~Node(){delete next;}
};
template <class T>
class SinglyLL
{
struct Node //not a template
{
T data;
Node *next;
Node(){data = 0; next = NULL;}
Node(const T &content){data = content;}
~Node(){delete next;}
};
//...
template <class T>
void SinglyLL<T>::add(T n)
{
Node<n> *temp = new Node(); //what?
I'm Using the Outer one... Sorry that I've posted it in hurry so i forgot to remove the inner Node structure before posting...
I'm using Visual Studio 2010 c++...
Apr 18, 2011 at 10:24am UTC
Then it should be Node<T> *temp = new Node<T>();
Apr 19, 2011 at 4:10pm UTC
i have tried that it isn't working...
Apr 20, 2011 at 12:21am UTC
You don't need that inner struct definition for it to be visible in the SinglyLL class. Changing references where the Node is used to Node<T> should get you started without any compilation problems. Just did on my VS 2010 Ultimate.
[Hint: check your destructor & remove functions. ]
Apr 20, 2011 at 6:16pm UTC
Thanks To all of u Guys...
Topic archived. No new replies allowed.