Operator Overloading << in c++
Posted
by
thlgood
on Stack Overflow
See other posts from Stack Overflow
or by thlgood
Published on 2012-06-27T02:56:54Z
Indexed on
2012/06/27
3:16 UTC
Read the original article
Hit count: 140
I'm a fresh man in C++.
I write this simple program to practice Overlaoding
.
This is my code:
#include <iostream>
#include <string>
using namespace std;
class sex_t
{
private:
char __sex__;
public:
sex_t(char sex_v = 'M'):__sex__(sex_v)
{
if (sex_v != 'M' && sex_v != 'F')
{
cerr << "Sex type error!" << sex_v << endl;
__sex__ = 'M';
}
}
const ostream& operator << (const ostream& stream)
{
if (__sex__ == 'M')
cout << "Male";
else
cout << "Female";
return stream;
}
};
int main(int argc, char *argv[])
{
sex_t me('M');
cout << me << endl;
return 0;
}
When I compiler it, It print a lots of error message:
The error message was in a mess.
It's too hard for me to found useful message
sex.cpp: ???‘int main(int, char**)’?:
sex.cpp:32:10: ??: ‘operator<<’?‘std::cout << me’?????
sex.cpp:32:10: ??: ???:
/usr/include/c++/4.6/ostream:110:7: ??: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostre
© Stack Overflow or respective owner