Can't print Linked List

Hi, I'm trying to write a function that will print out my unsorted Linked List.
When I compile the program I get the fault reading
"no operator "<<" matches these operands"
and
"binary "<<" no operator found which takes a right-hand operand of type 'ItemType' (or there is no acceptable conversion)

The program does not accept the line:

std::cout << currentPos->info << endl;

I will appreciate any help. Thank you.

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
#include <iostream>
#include <cstddef>

using namespace std;

enum RelationType { LESS, GREATER, EQUAL };

class ItemType {

public:
	ItemType();
	~ItemType() {};
	RelationType ComparedTo(ItemType otherItem) const;
	void GetValue() const;
	void Initialize(int number);

private:
	int value;
};

ItemType::ItemType()
{

	value = 0;
}

RelationType ItemType::ComparedTo(ItemType otherItem) const
{
	if (value < otherItem.value)
		return LESS;
	else if (value > otherItem.value)
		return GREATER;
	else return EQUAL;

}

void ItemType::GetValue() const
{
	cout << value << endl;
}


void ItemType::Initialize(int number) {
	value = number;
}

struct NodeType {

	ItemType info;
	NodeType* next;
};

class UnsortedType{
	public:
		friend struct NodeType;
		UnsortedType();
		void MakeEmpty();
		bool IsFull() const;
		int GetLength() const;
		ItemType GetItem();
		ItemType FindItem(ItemType item, bool &found);
		void PutItem(ItemType item);
		void ResetList();
		ItemType GetNextItem();
		void Print();
		
	private:
		NodeType* listData;
		int length; 
		NodeType* currentPos; 
		NodeType* nextPtr; 
		NodeType* firstPtr;
		NodeType* lastPtr; 
	
};

UnsortedType::UnsortedType()  {
	firstPtr = nullptr;
	lastPtr = nullptr; 
	listData = nullptr; 
	length = 0;
}

void UnsortedType::MakeEmpty() {

	NodeType* tempPtr;

	while (listData != NULL)
	{
		tempPtr = listData;
		listData = listData->next;
		delete tempPtr;

	}
	length = 0;
}

bool UnsortedType::IsFull() const
{
	NodeType* location;
	try
	{
		location = new NodeType;
		delete location;
		return false;
	}
	catch (bad_alloc exception)
	{

		return true;
	}
}

int UnsortedType::GetLength() const
{
	return length; 
}


ItemType UnsortedType::FindItem(ItemType item, bool& found)
{
	bool moreToSearch;
	NodeType* location;
	found = false;
	location = listData;
	moreToSearch = (location != NULL);
	
	while(moreToSearch && !found)
    {
    	switch(item.ComparedTo(location->info))
{
	case LESS:
	case GREATER: location = location->next;
		moreToSearch = (location != NULL);
		break;
	case EQUAL: found = true;
		item = location->info;
		break;
	
		}    	
		}	
		return item;
}

void UnsortedType::PutItem(ItemType item)
{
	NodeType* location;
	location = new NodeType;
	location->info = item;
	location->next = listData;
	listData = location; // aim ListData at new node 
	length++;
	
}

void UnsortedType::ResetList()
{
	currentPos = NULL; 
	
}

ItemType UnsortedType::GetNextItem()
{
	if(currentPos==NULL)
		currentPos = listData;
		else
		currentPos = currentPos->next;
		return currentPos->info;
	
}


void UnsortedType::Print() {
		
	if (GetLength() == 0) 
	{
	cout << "The list is empty." << endl;}
	NodeType *currentPos = listData;
	cout<<"The list is: "<<endl;
	while(currentPos!= nullptr)
	{
	}
	
}

int main() {
	UnsortedType List;
	UnsortedType List1;
	UnsortedType List2;
	ItemType item;
	ItemType item1; 
	ItemType item2;
	ItemType item3;
	ItemType item4;
	ItemType item5;

	item.Initialize(4);
	item1.Initialize(1);
	item2.Initialize(6);
	item3.Initialize(2);
	item4.Initialize(7);
	item5.Initialize(3);

	List.PutItem(item1);
	List.PutItem(item2);
	List.PutItem(item3);
	List.PutItem(item4);
	List.PutItem(item5);
	
	cout << "Current Items in List:" << endl; 
	List.Print();
	
	return 0;
}
Last edited on
Hi welcome,

You should post your code in code tags.

I was unable to get your code to run, so I did not troubleshoot it.

Below is an example I found here.
http://www.bogotobogo.com/cplusplus/linkedlist.php#linkedlistexample1

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
#include <iostream>

struct Node {
    int data;
    Node *next;
};

using namespace std;


// only for the 1st Node
void initNode(struct Node *head, int n) {
    head->data = n;
    head->next = NULL;
}

// apending
void addNode(struct Node *head, int n) {
    Node *newNode = new Node;
    newNode->data = n;
    newNode->next = NULL;

    Node *cur = head;
    while (cur) {
        if (cur->next == NULL) {
            cur->next = newNode;
            return;
        }
        cur = cur->next;
    }
}

void display(struct Node *head) {
    Node *list = head;
    while (list) {
        cout << list->data << " ";
        list = list->next;
    }
    cout << endl;
    cout << endl;
}


int main() {

    struct Node *newHead;
    struct Node *head = new Node;

    initNode(head, 10);
    addNode(head, 20);
    display(head);

}
Last edited on
Thank you. The link you sent me was very useful.
In order to print custom classes such as ItemType you must overload the std::ostream operator << for instance if I had a structure defined as follows:
1
2
3
4
5
6
struct X{
    int data;
    X(int a){
        data = a;
    }
};


and I wanted to print an instance of this class I would have:
1
2
3
std::ostream & operator<< (std::ostream &os, X &obj) {
	return os << obj.data;
}

declared so that the ostream object known as cout has a method of printing an instance of class X.

Oh and please edit your code and surround it with code tags like the following:
[code]Put code in between here...[/code]
Last edited on
This is very useful. Thank you!
What would be even more useful is if you did as you've already been asked (twice!), and edit your original post to use code tags.
I understand the importance of using code tags. This post is old and my code is now completely different thanks to help of the people who replied to this post. I will certainly use code tags for future posts. I have marked this post as solved.
Topic archived. No new replies allowed.