Strings in a linked list

I am setting up a class inside of a linked list. And inside that class, i am bringing in a string. The problem is, that the code works until the string is brought into the code, then my linked list breaks.

Now, when I don't pass any strings in the main.cpp file and leave it out of the constructor in the students.h file, it works just fine. I have tried using std::string in my code, but that doesn't seem to help either.

Once I add it, I get this error:
In member function `void LinkedList<T>::appendNode(T) [with T = Students]':
36 instantiated from here
46 no matching function for call to `LinkedList<Students>::ListNode::ListNode()'
note candidates are: LinkedList<Students>::ListNode::ListNode(const LinkedList<Students>::ListNode&)

---------
36 is in the main.cpp file where it starts: list.appendNode(student01);
46 is in the LinkedList.h file where it starts: newNode = new ListNode;
---------

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
#include <iostream>
#include <string>
#include <iomanip>
#include "LinkedList.h"
#include "Students.h"
using namespace std;

int main()
{
    // Define a LinkedList object
    LinkedList<Students> list;
    
    // Define some Student objects.
    Students student01(127801, "John", 4.0);
    Students student02(127802, "Sue", 3.0);
    
    // Store the Student objects in the list.
    list.appendNode(student01);
    list.appendNode(student02);
    
    // Display the values in the list.
    cout << "Here are the students:\n";
    list.displayList();
    cout << endl;
    system("pause");
    return 0;
}


Students.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
// Specification file for the Students class
#ifndef Students_H
#define Students_H
#include <string>
#include <iostream>
using namespace std;

class Students;	// Forward Declaration

// Function Prototypes for Overloaded Stream Operators
ostream &operator << (ostream &, const Students &);
istream &operator >> (istream &, Students &);

class Students
{
private:
   int stId;           // Student id
   string stName;      // Student's name
   string stAddress;    // Student's Address
   float gpa;          // Student's GPA
public:
   // Constructor
   Students(int i = 0, string n, double g = 0.0)
      { stId = i;
        stName = n;
        gpa = g; }
	
   // Friends
	friend ostream &operator << (ostream &, const Students &);
	friend istream &operator >> (istream &, Students &);
};

#endif 



LinkedList.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
// A class template for holding a linked list.
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>     // For cout and NULL
using namespace std;

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

   ListNode *head;   // List head pointer

public:
   // Constructor
   LinkedList()
      { head = NULL; }
      
   // Destructor
   ~LinkedList();
      
   // Linked list operations
   void appendNode(T);
   void displayList() const;
};


//**************************************************
// appendNode appends a node containing the value  *
// pased into newValue, to the end of the list.    *
//**************************************************

template <class T>
void LinkedList<T>::appendNode(T newValue)
{
   ListNode *newNode;  // To point to a new node
   ListNode *nodePtr;  // To move through the list

   // Allocate a new node and store newValue there.
   newNode = new ListNode;
   newNode->value = newValue;
   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.
   {
      // Initialize 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;
   }
}

//**************************************************
// displayList shows the value                     *
// stored in each node of the linked list          *
// pointed to by head.                             *
//**************************************************

template <class T>
void LinkedList<T>::displayList() const
{
   ListNode *nodePtr;  // To move through the list

   // Position 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 << endl;

      // Move to the next node.
      nodePtr = nodePtr->next;
   }
}
#endif 
Last edited on
If it would be easier, then in could go with char. Just don't know how.

Update: I tried it with char, but that resulted with the same errors above. I can't get the program to run with any type of text.
Last edited on
I'm not 100% convinced that the error you mention actually matches the code
you posted.

For example
In the Student class -in the constructor - the way you
have have the dafault parameters is incorrect.

1
2
3
public:
   // Constructor
   Students(int i = 0, string n, double g = 0.0) // ERROR  - cannot have gaps in default parameters  


It should be like this
1
2
3
public:
   // Constructor
   Students(int i = 0, string n ="", double g = 0.0) //give the string a default parameter  
Topic archived. No new replies allowed.