Linked List Issues

Hello, I have a program that requires me to create an object, populate it with information, and then append that objects info to a linked list. I was able to do this successfully. However, none of my other LinkedList functions (displaylist, deletenode, and insert node) work. I really just need to be able to display this information from the LinkedList itself. Any help would be much appreciated.

Linked.h
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#ifndef LINKEDLIST_H
 #define LINKEDLIST_H
 #include <iostream>
 using namespace std; 

 template <class L>
 class LinkedList
 {
 private:
 // Declare a structure for the list
 struct List
 {
    L value; // The value in the node
    struct List *next; // To point to the next node
 }; 

 	List *head; // The head pointer 

 public:

 // Constructor
 LinkedList()
 {
    head = NULL; // Establishes empty linked list
 } 

 // Destructor
 ~LinkedList(); 

 // Linked list operations
 void appendNode(L);
 void insertNode(L);
 void deleteNode(L);
 L searchList(int);
 void displayList() const; 

 }; 

 //***********************************************
 // appendNode function adds the node to the end *
 // list. It accepts an argument. *
 //***********************************************
 template <class L>
 void LinkedList<L>::appendNode(L num)
 {
 List *newNode; // To point to a new node
 List *nodePtr; // To move through the list 

 // Allocate a new node and store num there
 newNode = new List;
 newNode->value = num;
 newNode->next = NULL; 

 // If there are no nodes in the list make
 // newNode the first node
 if (!head)
 {
   head = newNode;
 }
 else // Otherwise, insert newNode at end
 {
 // Intialize nodePtr to head of list
nodePtr = head; 

// Find the last node in the list
while (nodePtr->next)
{
 nodePtr = nodePtr->next;
} 

// Insert newNode as the last node
nodePtr->next = newNode;
}
} 
 template <class L>
 void LinkedList<L>::insertNode(L num)
 {
 List *newNode; // A new node
 List *nodePtr; // To traverse the list
 List *previousNode = NULL; // The previous node 

 // Allocate a new node and store num there
 newNode = new List;
 newNode->value = num; 

 // If there are no nodes in the list make
 // newNode the first node
 if (!head)
 {
 head = newNode;
 newNode->next = NULL;
 }
 else // Otherwise, insert newNode
 {
 // Position nodePtr at the head of list
 nodePtr = head; 

 // Initialize previousNode to NULL
 previousNode = NULL; 

// Skip all nodes whose value is less than num
while (nodePtr != NULL && nodePtr->value < num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
} 

// If the new node is to be the 1st in the list
// insert it before all other nodes
if (previousNode == NULL)
{
head = newNode;
newNode->next = nodePtr;
}
else // Otherwise insert after the previous node
{
previousNode->next = newNode;
newNode->next = nodePtr;
  }
 }
}

template <class L>
void LinkedList<L>::deleteNode(L num)
{
List *nodePtr; // To traverse the list
List *previousNode; // To point to the previous node 

// If the list is empty, do nothing
if (!head)
{
return;
} 

// Determine if the first node is the one
if (head->value == num)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
// Intialize nodePtr to head of list
nodePtr = head; 

// Skip all nodes whose value member is not
// equal to num
while (nodePtr != NULL && nodePtr->value != num)
{
 previousNode = nodePtr;
 nodePtr = nodePtr->next;
} 

if (nodePtr)
{
previousNode->next = nodePtr->next;
delete nodePtr;
  }
 }
} 
template <class L>
void LinkedList<L>::displayList() const
{
List *nodePtr; // To move through the list 

// Postion nodePtr at the head of the list
nodePtr = head; 

 // While nodePtr points to a node, traverse the list
 while (nodePtr)
 {
 // Display the value in this node
 cout << nodePtr->value << " "; 

 // Move to the next node
 nodePtr = nodePtr->next;
 }
 } 
 template <class L>
 L LinkedList<L>::searchList(int value)
 {
 List *nodePtr; // To move through the list
 int location = -1;
 if (!head)
 {
 cout << "Element not found in list." << endl;
 return -1;
 } 

 // Position nodePtr at the head of the list
 nodePtr = head; 

 // While nodePtr points to a node, traverse the list
 while (nodePtr)
 {
 location++;

  // Determine if nodePrt is the one
  if (nodePtr->value == value)
  {
  return location;
  }
  else // If not move to the next one
  {
  nodePtr = nodePtr->next;
  }
 }
   return -1;
 } 
 template <class L>
 LinkedList<L>::~LinkedList()
 {
 List *nodePtr; // To traverse the list
 List *nextNode; // To point to the next node 

 // Position nodePtr at the head of the list
 nodePtr = head; 

 // While nodePtr is not at the end of the list
 while (nodePtr != NULL)
 {
 // Save a pointer to the next node
 nextNode = nodePtr->next; 

 // Delete the current node
 delete nodePtr; 

 // Position nodePtr at the next node
  nodePtr = nextNode;
  }
 }
 #endif  


WeatherStats.h
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
 #ifndef WEATHERSTATS_H
 #define WEATHERSTATS_H 

 class WeatherStats
 {
 private:
 double rainFall, // To hold rain fall amount
 snowFall, // To hold snow fall amount
 sunShine; // To hold amount of sunshine 

 public:
 // Default constructor
 WeatherStats(); 

 // Constructor
 WeatherStats(double, double, double); 

  // Mutators
   void setRainFall(double);
   void setSnowFall(double);
   void setSunShine(double); 

 // Accessors
   double getRainFall();
   double getSnowFall();
   double getSunShine();
 }; 

 //***********************************************
 // Default Constructor initializes the data *
 // members to 0. *
 //***********************************************
 WeatherStats::WeatherStats()
 {
  rainFall = 0;
   snowFall = 0;
   sunShine = 0;
 } 

//***********************************************
// Constructor that set the data. *
//***********************************************
WeatherStats::WeatherStats(double rain, double snow,
double sun)
{
  rainFall = rain;
  snowFall = snow;
  sunShine = sun;
} 

//***********************************************
// setRainFall function sets the amount of *
// rainfall for a given month *
//***********************************************
void WeatherStats::setRainFall(double rf)
{
 rainFall = rf;
} 

//***********************************************
// setSnowFall function sets the amount of *
// snowfall for a given month *
//***********************************************
void WeatherStats::setSnowFall(double sf)
{
 snowFall = sf;
} 

//***********************************************
// setSunShine function sets the amount of *
// sunshine for a given month *
//***********************************************
void WeatherStats::setSunShine(double ss)
{
 sunShine = ss;
} 

//***********************************************
// getRainFall function returns the amount of *
// rainfall for a given month *
//***********************************************
double WeatherStats::getRainFall()
{
return rainFall;
} 

//***********************************************
// getSnowFall function returns the amount of *
// snowfall for a given month *
//***********************************************
double WeatherStats::getSnowFall()
{
return snowFall;
} 

//***********************************************
// getSunShine function returns the amount of *
// sunshine for a given month *
//***********************************************
double WeatherStats::getSunShine()
{
return sunShine;
} 

#endif  


main.cpp

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
#include "WeatherStats.h"
#include "LinkedList.h"
#include <iostream>
#include <list>

using namespace std;

int main()
{
 int months;    // To hold number of months
 double rainFall;
 double snowFall;
 double sunShine;

// Create a LinkedList object
LinkedList<WeatherStats> weather;

// Define some WeatherStats objects
WeatherStats forecast;

// Get input from user 
cout << "How many months do you wish to enter\n"
     << "weather statistics for? ";
cin >> months;

for (int i = 0; i < months; i++)
{
cout << "Enter the amounts for month " << i+1 << endl;
cout << "Rainfall: ";
cin >> rainFall;
cout << "Snowfall: ";
cin >> snowFall;
cout << "Sunshine: ";
cin >> sunShine;
cout << endl;

// Store the amounts
forecast.setRainFall(rainFall);
forecast.setSnowFall(snowFall);
forecast.setSunShine(sunShine);

// Append the amounts
cout<<endl;

}
weather.appendNode(forecast);
weather.displayList(); // <---This is where the error occurs

system("Pause");
}


The error I get is as follows:

Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'WeatherStats' (or there is no acceptable conversion)

Anyone know what's going on?
You are trying to display a list of WeatherStats, which tries to output the data using operator <<, but no such operator exists for your WeatherStats class.
Hello firedraco, thank you for your response. So what you're saying is I need to make an overloaded << function to output values in my WeatherStats class in order to display them from the linked list?
Topic archived. No new replies allowed.