Error storing objects of a class in a linked list

Confused on why I'm getting an error when I try to store information into a node of type Student (they're different data types so I figured this would be best) and then use the class to store the information into a linked list node....

The error I'm getting is:
Error: Invalid operands to binary expression('basic_ostream<char>, std::__1char_traits<char> >' and "Student')


Why is this happening??

Here's the Student.h file
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
#ifndef STUDENT_H
#define STUDENT_H

using namespace std;
class Student
{
    private:
        int studentID;
        string studentName,
               studentAddress;
        float studentGPA,
              studentAverage;

    public:
        Student(int i = 0, string n = " ", string a = " ", float g = 0.0, float av = 0.0)
        {
            studentID = i;
            studentName = n;
            studentAddress = a;
            studentGPA = g;
            studentAverage = av;
        }

        void setStudentID(int i)
        {
            studentID = i;
        }

        void setStudentName(string n)
        {
            studentName = n;
        }

        void setStudentAddress(string a)
        {
            studentAddress = a;
        }

        void setStudentGPA(float g)
        {
            studentGPA = g;
        }

        void setStudentAverage(float av)
        {
            studentAverage = av;
        }

        int getStudentID() const
        {
            return studentID;
        }

        string getStudentName() const
        {
            return studentName;
        }

        string getStudentAddress() const
        {
            return studentAddress;
        }

        float getStudentGPA() const
        {
            return studentGPA;
        }

        float getStudentAverage() const
        {
            return studentAverage;
        }

};
#endif 


LinkedList.h file
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
#ifndef LINKEDLIST_H
#define LINKEDLIST_H

#include <iostream>
using namespace std;

template <class T>
class LinkedList
{
    private:
        struct ListNode
        {
            T value;

             struct ListNode *next;
        };

        ListNode *head;

    public:
        LinkedList()
        {
            head = nullptr;
        }

        ~LinkedList();

        void appendNode(T);
        void displayList();
};

template <class T>
void LinkedList<T>::appendNode(T newValue)
{
    ListNode *newNode;
    ListNode *nodePtr;

    newNode = new ListNode;
    newNode->value = newValue;
    newNode->next = nullptr;

    if (!head)
    {
        head = newNode;
    }
    else
    {
        nodePtr = head;

        while(nodePtr->next)
        {
            nodePtr = nodePtr->next;
        }

        nodePtr->next = newNode;
    }
}

template <class T>
void LinkedList<T>::displayList()
{
    ListNode *nodePtr;

    nodePtr = head;

    while (nodePtr)
    {
        cout << "Student Info: " << nodePtr->value << endl;

        nodePtr = nodePtr->next;

        cout << "\n";
    }
}
#endif 


and my cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <list>
#include "LinkedList.h"
#include "Student.h"

using namespace std;

int main()
{
    Student student1(23402394, "Student One", "Address 1", 2.45, 70);
    Student student2(12312312, "Student Two", "Address 2", 3.78, 93);

    LinkedList<Student> list;
    list.appendNode(student1);
    list.appendNode(student2);

    cout << "-----------------------------------------" << endl;

    list.displayList();

    return 0;
}

Last edited on
cout << "Student Info: " << nodePtr->value << endl;

Here, you are directing an object of type Student towards cout.

cout has no idea what to do with an object of type Student.

It knows how to deal with strings, and ints, and doubles, and chars, and various other kinds of object. It has no idea what to do with a Student.
But isn't Student an object of type class? So it should be able to access that type?
It knows how to access specific types of object, for which a << operator has been defined.

But it knows nothing about your Student type. How could it? How could it magically know how you want your class written out?

If you want to be able to stream your class like that, you need to write a << operator for it.
So it should be able to access that type?


And do what with it? What's it supposed to do with it? As Mikey says, is it meant to magically know how you want each internal value displayed? And in what order? And which ones to leave out?
Last edited on
Makes sense! I added this to produce output but now it won't run... I've never done overloaded operators so I'm not sure why this isn't working.

1
2
3
4
5
       friend ostream &operator << (ostream& cout, const Student &student)
        {
            cout << "Student ID: " << student.studentID << "Student Name: " << student.studentName;
            return cout;
        }
Will it not run, or will it not compile?
It doesn't give me any errors when it compiles but it doesn't execute the program.
It shouldn't compile because on line 26 of LinkedList.h you promise to write a destructor, but you never do (or more accurately, it should compile but fail to link).

If your compiler/linker isn't telling you that, there's something wrong with your compiler/linker/combined-workflow or how you're using it.
Last edited on
Topic archived. No new replies allowed.