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){}
  • 5
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值