Sou*_*aha -1 c++ namespaces compiler-errors
当我尝试编译此代码时
using namespace std;
namespace asf{
inline int operator|(int);
}
asf::operator|(int x){
return (x>1)?x*operator|(x-1):1;
}
int main(){
cout<<5|;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
[Error] 'int asf::operator|(int)' must have an argument of class or enumerated type
[Error] ISO C++ forbids declaration of 'operator|' with no type [-fpermissive]
[Error] 'int asf::operator|(int)' should have been declared inside 'asf'
[Error] 'int asf::operator|(int)' must have an argument of class or enumerated type
In function 'int main()':
[Error] expected primary-expression before ';' token
Run Code Online (Sandbox Code Playgroud)
怎么了?请帮忙.
正如错误所述,重载运算符必须至少有一个类或枚举类型的参数.这就是语言的运作方式.
此外,重载时无法更改运算符的arity.你试图定义一个一元|,这也是非法的.|必须总是拿两个论点.声明operator |只有在一个类中声明它才能包含一个参数,在这种情况下,左手操作数隐含在类的类型中.
| 归档时间: |
|
| 查看次数: |
78 次 |
| 最近记录: |