This line is giving me an error: cout<<"List: "<<list<<endl;

I really do not understand why I am getting an error here on this line.
Because on the other lines before it I do not get an error.

Here is the error:
114 16 C:\Martin\MalikChapter8\main.cpp [Error] no match for 'operator<<' (operand types are 'std::basic_ostream<char>' and 'linkedListType<int>')

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
 #include <iostream>
#include "linkedList.h"
#include "queueLinked.h"
using namespace std;

int main()
{
	linkedListType<int> list;
	int num;
	
	cout<<"Enter numbers ending wiht -999" <<endl;
	
	cin >> num;
	
	while(num != -999)
	{
		list.insertLast(num);
		cin >> num;
	}
	
	cout << endl;
	
	cout<<"List: "<<list<<endl;
	cout<<"Length of the list: "<<list.length() <<endl;
	
	cout<<"Enter all occurrences of number to be deleted: ";
	cin >> num;
	cout << endl;
	
	list.deleteAll(num);
	
	cout<<"List after deleting all occurrences of "<< num << endl;
	cout<<list<<endl;
	cout<<"Length of the list: "<<list.length()<<endl;
	
	list.deleteSmallest();
	cout<<"List after deleting the smallest element" << endl;
	cout<<list<<endl;
	cout<<"Length of the list: "<<list.length() <<endl;
	
	return 0;
}
The error is on line 23
The line number on your error message doesn't seem to match the line numbers in the code you've posted. Can you indicate exactly which line of code is causing the error?
In order to use linkedListType within a stream operation you need to overload the operator<<(...) for linkedListType.
So the error is on the very first line where you try to stream the object? In that case, yeah, you either haven't defined an << operator for your class, or you've done it wrong. Since you haven't shown us the definition of the class, we can't know which. (Why did you think withholding that code would be a helpful thing to do?)

The compiler can't magically know how you want to output your class. You have to tell it. You do that by defining the << operator for the class.
Last edited on
I really do not understand why
[Error] no match for 'operator<<' (operand types are 'std::basic_ostream<char>' and 'linkedListType<int>')

Operator<< and types --> there is code like:
1
2
3
std::basic_ostream<char> bar;  // cout is an ostream
linkedListType<int> foo;
bar << foo; // error 


no match for
The compiler says that it has looked that every operator<< that it knows about and none of them matches the case that bar << foo has.
Thank you all.
I eventually managed to overload the operator<< properly
Topic archived. No new replies allowed.