오버로드를 연습하는이 간단한 프로그램을 작성했습니다. 내가 컴파일 할 때 오류 메시지가 많이 인쇄연산자 오버로드 << C++
#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;
}
:
오류 메시지가 엉망이었다
이 내 코드입니다.
유용한 메시지를 찾지 못했습니다.
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
'cerr << "성별 오류!" << sex_v' 네 말이 맞아. 나는 그 위치에 대해 들어 본 적이 없어. – chris
모든 진지하게, 반드시하지 말아야 할 것은 두 개의 밑줄을 사용하는 것입니다. 이것은 구현을 위해 예약되어 있습니다. – chris
어떻게해야합니까? '_sex_' 또는 다른 것을 사용 하시겠습니까? – thlgood