编译错误

 温暖白 发布于 2023-02-09 14:05

当我尝试编译此代码时

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|;
}

我收到以下错误

[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

怎么了?请帮忙.

1 个回答
  • 正如错误所述,重载运算符必须至少有一个类或枚举类型的参数.这就是语言的运作方式.

    此外,重载时无法更改运算符的arity.你试图定义一个一元|,这也是非法的.|必须总是拿两个论点.声明operator |只有在一个类中声明它才能包含一个参数,在这种情况下,左手操作数隐含在类的类型中.

    2023-02-09 14:07 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有