C++高级编程一书说,可以如下这样重载操作符,但是我编译的时候出错了。
friend const SpreadsheetCell& operator+(const SpreadsheetCell& rth, const SpreadsheetCell& lth);
const SpreadsheetCell SpreadsheetCell::operator +(const SpreadsheetCell& rhs, const SpreadsheetCell& lhs)
{
SpreadsheetCell newCell;
newCell.set(lhs.mValue + rhs.mValue);
return (newCell);
}
编译出错如下:
G:\qt\sheet-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK____\..\Sheet\SpreadsheetCell.cpp:45: 错误:'const SpreadsheetCell SpreadsheetCell::operator+(const SpreadsheetCell&, const SpreadsheetCell&)' must take either zero or one argument
说的大概就是只能有0个或者一个参数,不知道为什么。。
本文探讨了C++中操作符重载的正确用法,特别是针对两个参数的操作符+的实现方式。作者尝试定义了一个名为SpreadsheetCell的类,并在其中重载了操作符+,但在编译过程中遇到了错误。
1444

被折叠的 条评论
为什么被折叠?



