为什么我在这段代码中遇到错误?错误是:'long'之前预期的主要表达式

Nit*_*hra 1 c++ object type-conversion

Int在将类的成员数据转换为long double转换运算符时,我在这段代码中遇到错误。但我不明白为什么会出现这种情况?

 #include<iostream>
  using namespace std;

 class Int
 {
    private:
         int number;
    public:
        Int() : number(0)                                         // no argument constructor
     {     }

     Int( int i) : number(i)                                 // 1-argument constructor
       {     }

    void putInt()                                           // display Int
     { cout << number; }

     void getInt()                                           // take value from the user
     { cin >> number; }

     operator int()                                           // conversion operator ( Int to int)
     { return number; }

     Int operator + ( Int a)
     { return checkit( long double (number) + long double (a))  ; }  // performs addition of two objects of type Int
    
     Int operator - ( Int a)
     { return checkit(long double (number) - long double (a) ); }  //performs subtraction of two objects of type Int

     Int operator * (Int a)  
     { return checkit( long double (number) * long double (a) ); }  // performs multiplication of two objects of type Int

     Int operator / (Int a)
     { return checkit( long double (number) / long double (a) ); } // performs division of two objects of type Int

     Int checkit( long double answer)
     {
        if( answer > 2147483647.0L || answer < - 2147483647.0L)
          { cout << "\nOverflow Error\n";
             exit(1);
          }

        return Int ( int(answer));
     }
   
 }; 

 int main()
  {
    Int numb1 = 20;
    Int numb2 = 7;
    Int result, cNumber;

    result = numb1 + numb2;
   cout << "\nresult = "; result.putInt();                         //27
  result = numb1 - numb2;
  cout << "\nresult = "; result.putInt();                         //13
   result = numb1 * numb2;
  cout << "\nresult = "; result.putInt();                        // 140
   result = numb1 / numb2;
  cout << "\nresult = "; result.putInt();                        // 2

  cNumber = 2147483647;
   result = numb1 + cNumber;     // overflow error
   cout << "\nresult = "; result.putInt();
   cNumber = -2147483647;
  result = numb1 + cNumber;                                      // overflow error

  cout << endl;
   return 0;

  }
Run Code Online (Sandbox Code Playgroud)

+在、-和运算符*的运算符重载代码中/,我收到以下错误:

'long' 之前的预期主要表达

我不明白为什么会发生这种情况。

Adr*_*ica 6

C++ 语法的性质不允许在函数转换中使用包含多个单词(例如long doubleand )的类型名称,正如您尝试在and表达式中所做的那样。(不过,只使用普通类型就可以了。)unsigned intlong double (number)long double (a)double

\n

但你确实不应该在 C++ 程序中使用此类强制转换 \xe2\x80\x93 它们几乎与 C 风格强制转换一样邪恶。

\n

只要有可能,就使用显式的 C++ 强制转换,从“最软”的可用选项开始;就您而言,static_cast将适用于int转换long double以及任何其他转换;因此,只需将二元运算符函数更改为如下形式:

\n
    Int operator + (Int a)\n    {\n        return checkit(static_cast<long double>(number) + static_cast<long double>(a));\n    }  // performs addition of two objects of type Int\n
Run Code Online (Sandbox Code Playgroud)\n