【发布时间】:2021-05-08 13:45:15
【问题描述】:
将“const IntBag”作为“this”参数传递会丢弃限定符 [-fpermissive]
这是我的代码: '''C++ **
> int IntBag::operator[](int index){ //get array item
> return array_[index];
> }
>
> IntBag::IntBag (const IntBag& a){ //copy constructr
> size=a.getSize();
> for(int i=0; i<size; i++){
> int val=a[i];
> array_[i]=val;
> }
> }
>
> int IntBag::getSize()const{ //get the size of the array
> return size;
> }
>
> IntBag& IntBag::operator = (const IntBag& a){
> for(int i= 0; i< a.getSize(); i++) {
> array_[i]=a[i];
> }
> }
**
我的错误是: $ g++ intbag.cpp intbag.cpp:在复制构造函数“IntBag::IntBag(const IntBag&)”中: intbag.cpp:19:16:错误:将“const IntBag”作为“this”参数传递会丢弃限定符 [-fpermissive] 19 | int val=a[i]; | ^ intbag.cpp:12:5: 注意:调用‘int IntBag::operator’ 12 | int IntBag::operator[](int index){ //获取数组项 | ^~~~~~ intbag.cpp:在成员函数“IntBag& IntBag::operator=(const IntBag&)”中: intbag.cpp:30:18:错误:将“const IntBag”作为“this”参数传递会丢弃限定符 [-fpermissive] 30 |数组_[i]=a[i]; | ^ intbag.cpp:12:5: 注意:调用‘int IntBag::operator’ 12 | int IntBag::operator[](int index){ //获取数组项 | ^~~~~~ intbag.cpp:32:1:警告:函数中没有返回语句返回非 void [-Wreturn-type] 31 | } +++ |+ 返回 *this; 32 | } | ^
【问题讨论】:
-
不要发布文字图片。而是复制文本。
-
不要在 C++ 问题中使用 C 标签。
标签: c++ arrays error-handling compiler-errors