如果<<
運算符超載的返回類型爲std::string
,我很難理解編譯器控制錯誤的原因。你能幫我理解嗎?錯誤<<運算符重載返回一個std ::字符串
波紋管是一個可重現的例子,它給出了一個巨大的錯誤。
class XY
{
int X__;
int Y__;
public:
XY(int x, int y):X__(x), Y__(y){}
~XY(){}
std::string operator<<(const XY_cartesiano& c)
{
std::stringstream ss;
ss << "{ " << X__ << ", " << Y__ << " }";
return ss.str();
}
int x() const{return X__;}
int y() const{return Y__;}
};
void main()
{
XY a(1,2);
std::cout << a;
}
這是沒有問題的,但包含兩個連續的下劃線('X__','Y__')和名稱的名稱以下劃線跟着一個大寫字母開頭保留供實施使用。不要在你的代碼中使用它們。 –