Problem with ostream overloading

Okay. I have searched both my textbook and the internet for the past few hours, and I've made progress in various ways - but I'm still having a problem with my code to overload the ostream operator (<<).

I'm trying to include all relevent information, please ask if there's anything I left out. Most likely it's going to be one of those really, really stupid, simple things... so any and all help appreciated. This is part of a uni assignment, which is 95% done, except for this one function...

I have a class called set, with various functions declared and working. I'm now trying to use the ostream overloader to output the contents of a set.

Here is the declaration in the header file:
 
friend ostream operator<<(ostream &os, const set mySet);


Private data for class set:
1
2
3
private:
  vector <bool> setlist;  // indicates set elements as true
  int cardinality; // number of (true) items in set 


And here is the code I have in set.cpp:
1
2
3
4
5
ostream operator<<(ostream &os, const set mySet)
{
    mySet.display();
    return os;
}


It is on line 4 of the code in set.cpp that I'm encountering problems. There's a squiggly red line underneath it, it won't compile, and the error message is "within this context".

Thanks in advance :)
The insertion operator should be defined as:
1
2
3
4
5
ostream& operator<<( ostream& outs, const myType& myData )
{
    ...
    return outs;
}
Notice how everything is passed by reference.

BTW, avoid variable names like 'os', because that gives you 'is' for the extraction operator (>>), and using 'is' for variable names is a bad idea, even in C++.

Hope this helps.

[edit] One more thing. How does mySet.display(); write to the argument ostream?
Last edited on
Thankyou so much. It appears to be working now. :)
One more thing. How does mySet.display(); write to the argument ostream?

I'm sorry, I don't understand the question. ? Display is a void function that outputs the data (I'm thinking I will replace that line with the actual display function code).
Last edited on
The whole point of the insertion operator is to write data to the argument stream (which you have named 'os'). To what stream does mySet.display(); write? It certainly can't be 'os'...
Last edited on
Maybe I'm being particularly dim tonight (in which case I should probably remove myself from coding until I've had some sleep) but what you're asking is just going way over my head. I'm only a beginner at C++, and we haven't covered anything in class about writing data.

Here is the function display, if that helps answer your question. (And it's just now that I remember that display is a hideously written piece of inefficient code... but it worked, and for the purpose of submitting the assignment that was good enough at the time.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 void set::display()const
{
   // pre : none
   // displays the elements of this set enclosed in braces and separated by commas eg {1,2,3} or {}
   //(do not use backspace, delete etc)
	unsigned int highest = getLargest();

	cout << "{";
	if (cardinality == 0)
		cout << "}" << endl;
	for (unsigned int i = 0; i < setlist.size(); i++)
	{
		if (i == highest)
			cout << highest << "}" << endl;
		else if (setlist.at(i) == true)
			cout << i << ",";
	}
} 


I also took your advice and renamed os.
Topic archived. No new replies allowed.