Overloading operator << for use with iterator and array

Hello, I need help with an error with my overloaded operator <<.
The following works for my vector which uses array indexing to output the object, but the set which uses the iterator doesn't work.

1
2
vector<MyClass> myVector;
set<MyClass> mySet;


1
2
3
4
5
6
std::ostream& operator<<(std::ostream &os, MyClass &myClass)
{
	os << "The name is: " << myClass.name;

	return os;
}


1
2
3
4
for(int i=0; i<myVector.size();i++)
{
	cout << myVector[i] << endl; //no error and outputs perfectly
}


1
2
3
4
for(set<MyClass>::iterator it=mySet.begin(); it!=mySet.end();it++)
{
	cout << *it << endl; //error
}
Last edited on
Maybe this will point you in the right direction:

From here: http://www.cplusplus.com/reference/set/set/?kw=set
The value of the elements in a set cannot be modified once in the container (the elements are always const)
Thank you, norm.
I was struggling for such a long time, yet that's all I had to do.
Topic archived. No new replies allowed.