Original Post
I have a problem with teaching cout how to recognize my objects. Let's take a very basic example:
#include <iostream>
using namespace std;
class Example
{
private:
int m_x;
public:
Example(int = 1);
friend void operator<<(ostream &, Example);
};
int main()
{
Example overloading;
cout << overloading;
return 0;
}
Example::Example(int i)
{
m_x = i;
}
void operator<<(ostream & os, Example e)
{
os << e.m_x;
}
The above snippet of code works as intended, but being willing to experiment, I made one modification to it and it broke. Namely, I removed the & reference-to operator from the operator<< parameter list so that the whole function header and the prototype read like this:
void operator<<(ostream os, Example e)
Can anyone explain why the code works with ostream & but not with plain ostream? Thanks.
[Edit] Forgot to add, here's the error message:
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Example' (or there is no acceptable conversion).
[edited by - fallenang3l on April 13, 2002 2:42:23 PM]