Overloading << & >> for an object

Microsoft Visual Studio is telling me, with a red squiggly under operator, "Error: too many parameters for this operator function." I looked up how to overload the operators via this link:
(http://stackoverflow.com/questions/4421706/operator-overloading/4421719#4421719)

Following the syntax in those examples isn't fixing the error.

Inside the Set class, .h file is as follows. (prototypes)
1
2
3
4
5
6
7
8
9
//Top of .h file
#include <string>
#include <iostream>

//... More code here
 
//Inside Set Public:
 std::ostream & operator<<(std::ostream&, Set &);
 std::istream & operator>>(std::istream& is, int);


Implementation inside the Set.cpp file, is as follows.
1
2
3
4
5
6
7
8
9
//Already overloaded [ ] to return [x] element of the set
std::ostream & operator<<(std::ostream& os, Set & a){
	
	for (int i = 0; i < a.size(); i++){
		os << a[i];
	}

	return os;
}


Thanks for the help.
try taking making them friend functions and not public members.
Ignore the squiggly lines - actually compile the code, copy and paste the exact error you get, and indicate exactly which line the error is on.
@pepstein

If I move my overloaded operator functions outside my class, or leave them inside with the friend notation in front they work, but with some unexpected residual affects. The >> operator, when overloaded, now doesn't work with the cin statements inside my main function. It says:

" 5 IntelliSense: more than one operator ">>" matches these operands:
function "operator>>(std::istream &is, int)"
function "std::basic_istream<_Elem, _Traits>::operator>>(int &_Val) [with _Elem=char, _Traits=std::char_traits<char>]"
operand types are: std::istream >> int
"

My cout << object; does work now though.

@LB

With the two lines (8 and 9) commented out, my program runs without error. With them in, it won't compile giving the following errors:

Error 1 error C2804: binary 'operator <<' has too many parameters
Error 3 error C2804: binary 'operator <<' has too many parameters

This error refers to the call in the main function.
Error 4 error C2679: binary '<<' : no operator found which takes a right- hand operand of type 'Set' (or there is no acceptable conversion)

 
cout << object;



Thanks for the reply's.
your istream's second parameter should be accepting an object of the class not an int, similarly to the ostream.
Problem solved! Thank you!
Topic archived. No new replies allowed.