OJ做題,顯示錯誤積累

(1)error: only constructors take member initializers

初始化列表只可以在構造函數中使用,常見錯誤,把類名打錯了
如:Data Date 和Point Ponit

(2)error: no matching function for call to 'Point::Point()

構造函數,少了一種類型(參數列表對不上)

(3)error: expected unqualified-id before ‘int’

C++不能在同一條語句中定義不同類型的變量。

(4)error: expected primary-expression before ‘)’ token

錯誤代碼:for(i=t.h;i<now.h;i- -)
- -不能分開寫,需要寫爲--

(5)error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘std::_Setfill’)

注意:
setw(w) :設置數據的輸出寬度爲w個字符
setfill©:設置用字符c作爲填充字符
所以不能寫成
setfill(0)
而應該是
setfill(‘0’)
區分:setw(2),是正確寫法,與它的不同

(6)error: expected unqualified-id at end of input|

錯誤行數出現在代碼最後,說明前面有函數少了括號
需要從頭檢查一下括號情況

(7)error: call of overloaded ‘DateTime(Date&, Time&)’ is ambiguous|

ambiguous是模棱兩可的意思,說明,電腦不知道調用哪一個合適了,需要尋找“重複部分
注意

//   DateTime(Date &a,Time &b):da(a),t(b){}
    DateTime(const Date& a,const Time& b):da(a),t(b){}
    ```
   上述兩個語句,不    Point ( Point & a):m(a.m),n(a.n){sum++;now++;t=now;}是構成“重複”
   但是,下面的不行

//DateTime(Date a,Time b):da(a),t(b){}
DateTime(const Date& a,const Time& b):da(a),t(b){}
```
處理方法:可以註釋掉一個,或者加上&

(8)error: invalid initialization of non-const reference of type ‘Point&’ from an rvalue of type ‘Point’

c++中臨時變量不能作爲非const的引用參數

//    Point ( Point & a):m(a.m),n(a.n){sum++;now++;t=now;}

只需要改爲
const Point & a

注意
平時傳參可以一般用傳引用,其次加const

(9)error: passing ‘const Point’ as ‘this’ argument discards qualifiers [-fpermissive]|

[下例,來自百度】
int getX() const,int getY() cosnt。
編譯器認爲調用t的getX()和getY()函數,在函數中可能修改了t的成員變量值,這違反了const修飾符。因此只允許調用t的const函數。

也可能是,參數可能不是const類型從而出錯

把成員函數也用const,表明函數體內部沒有賦值語句
形參爲const的類,其成員函數既不能修改形參,且必須聲明爲const函數。如果函數體內,沒有改變值,那麼最好習慣加上const

(11)error: non-member function ‘void ShowPoint(const Point&)’ cannot have cv-qualifier|

在C++中,非成員函數不能含有CV限定,即const和volatile限定

//void ShowPoint (const Point & a)const{}
//應改爲
void ShowPoint (const Point & a){}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章