[英]Compiling Error
當我嘗試編譯此代碼時
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
怎么了? 請幫忙。
如錯誤所述,重載的運算符必須至少具有一個類或枚舉類型的參數。 這就是語言的工作方式。
另外,重載時不能更改運算符的屬性。 您正在嘗試定義一元|
,這也是非法的。 |
必須始終接受兩個參數。 operator |
的聲明operator |
僅當在類中聲明了一個參數時,它才可以包含一個參數,在這種情況下,左側操作數隱式屬於該類的類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.