Templated Linked List help
Apr 18, 2012 at 3:00am UTC
Greetings,
I am tasked with a challenging linked list assignment. As I'm reading through my book I'm trying to incorporate what I learn in my compiler as I go.
What I want to do is be able to loop through in main and create a linked list of however many nodes. But, my compiler is not happy.
1>Linked.cpp(33): error C2955: 'NodeList' : use of class template requires template argument list
1> Linked.cpp(11) : see declaration of 'NodeList'
1>Linked.cpp(41): error C2514: 'NodeList' : class has no constructors
1> Linked.cpp(11) : see declaration of 'NodeList'
And of course, here is my code:
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
#include <iostream>
#include <string>
#include <vector>
using namespace std;
template <typename T>
class NodeList
{
public :
NodeList(const T);
T getData()const ;
private :
T data;
NodeList<T> * nextPtr;
};
template <typename T>
NodeList<T>::NodeList(const T info):data(info), nextPtr(0){}
template <typename T>
T NodeList<T>::getData()const
{ return data;
}
int main()
{
NodeList * numberList = NULL;
double number;
cout << "Enter numbers: " ;
cin >> number;
while (cin >> number)
{
numberList = new NodeList(number, numberList);
}
system("PAUSE" );
return 0;
}
For whatever reason, linked lists are slippery in my brain (I blame the pointers). Any clarity that can be provided will be appreciated, of course.
Thanks
Apr 18, 2012 at 4:40am UTC
I solved this; there wasn't a second parameter declared in my constructor.
There is a second example I did from my book.
What's interesting is the first value I enter never makes it in to the link; it flies off in to some alternate universe.
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
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct ListNode
{
double value;
ListNode *next;
ListNode(double _value, ListNode *_next = NULL)
{
value = _value;
next = _next;
}
};
int main()
{
ListNode * numberList = NULL;
double number;
cout << "Enter numbers: " ;
cin >> number;
while (cin >> number)
{
cout << number << endl;
numberList = new ListNode(number, numberList);
}
cout << "The lists data is... *drum roll*" << endl;
ListNode *ptr = numberList;
while (ptr != NULL)
{
cout << ptr->value << " " ;
ptr = ptr->next;
}
cout << endl;
system("PAUSE" );
return 0;
}
Enter numbers: 1 2 3 4 5 6 7 8 9 10 barf
2
3
4
5
6
7
8
9
10
The lists data is... *drum roll*
10 9 8 7 6 5 4 3 2
Press any key to continue . . .
Does anyone know why the one doesn't get linked?
Apr 18, 2012 at 10:58am UTC
On line 24 you read the first number which is on line 26 overwritten
Apr 18, 2012 at 12:25pm UTC
Hi Coder,
When I comment out line 24 an exception is thrown
An unhandled exception of type 'System.NullReferenceException' occurred in Linked Lists.exe
Do you know why this happens? I am having a tough time with linked lists.
Apr 18, 2012 at 12:36pm UTC
Your code works fine.
'System.NullReferenceException' looks like a .NET thing. You probably created the wrong kind of project.
Apr 18, 2012 at 1:27pm UTC
Interesting.
You're right, it works in Dev. Thanks for letting me know.
Topic archived. No new replies allowed.