5 c++ compiler-errors operator-overloading
这很烦人,我可以用这些参数/返回写一个函数,但为什么我不能定义一个运算符来做这个?
-edit-我实际上是试图超载<<以下仅供参考.
// C2803.cpp
// compile with: /c
class A{};
bool operator< (const A *left, const A *right); // C2803
// try the following line instead
// bool operator< (const A& left, const A& right);
Run Code Online (Sandbox Code Playgroud)
gcc错误
error: ‘bool operator<(const A*, const A*)’ must have an argument of class or enumerated type
Run Code Online (Sandbox Code Playgroud)
Xeo*_*Xeo 10
因为每个用户定义的运算符重载都需要至少一个用户定义的类型作为参数.点不是用户定义的类型.
C++ 03标准,§13.5 [over.oper] p6:
运算符函数应该是非静态成员函数或者是非成员函数,并且至少有一个参数,其类型是类,对类的引用,枚举或对枚举的引用.
| 归档时间: |
|
| 查看次数: |
2449 次 |
| 最近记录: |