Having an error in program (binary operator <<)

I keep getting an error (will mark where below) and I'm not sure what is going on here. We have been introduced to overloading an output operator and I am not sure on the syntax of how to do this it seems. By the way, we are also working with templates. Here is my header 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
#ifndef LISTTYPE__H
#define LISTTYPE__H

using namespace std;
#include <iostream>

template <class T>
class ListType
{
public:
	ListType(size_t = 10);
	ListType(const ListType<T>&);
	virtual ~ListType();
	const ListType<T>& operator=(const ListType<T>&);
	virtual bool insert(const T&) = 0;
	virtual bool eraseAll();
	virtual bool erase(const T&) = 0;
	virtual bool find(const T&) const = 0;
	size_t size() const;
	bool empty() const;
	bool full() const;
	ostream& operator << (ostream&, const ListType<T>&); // ERROR IS HERE
protected:
	T *list;
	size_t capacity;
	size_t count;
	

};

#endif 


The error is:
binary 'operator <<' has too many parameters
Last edited on
I seem to have fixed it by removing the: ostream& portion of this line in my () parameters

Was this correct fix?
Last edited on
Was this correct fix?

No.

operator<< is a binary function, meaning it takes two arguments. In order for it to work properly as the insertion operator, the first argument must be an ostream type. If you removed the ostream& portion, I'm sure you'll find that output doesn't actually work.

When a function is defined as a member of a class, the first argument is always the this argument which is a pointer to the object the function was called on, therefore operator<< should not be defined as a member function when it is being used to implement the insertion operation on a stream. It should be defined as a non-member function.
Last edited on
Topic archived. No new replies allowed.