实参时丢弃了类型 discards qualifiers discards qualifiers问题

今晚中秋之夜,苦逼的程序员只能呆在寝室。

正无聊的时候,想起最近学的数据结构,想完善一下代码,于是想给线性表重载一下<<运算符。


Out()

[cpp]  view plain copy
  1. template<class T>  
  2.     void LineList<T>::Out(ostream& os)  
  3.     {  
  4.         for(int p = 0; p < length; p++)  
  5.             os<<array[p]<<endl;  
  6.     }  

重载<<

[cpp]  view plain copy
  1.     friend ostream& operator<<(ostream& out,const LineList<T>&line)  
  2.     {  
  3.         line.Out(out);  
  4.         return out;  
  5.     }  

但是问题出现了。。

[cpp]  view plain copy
  1. error:passing ‘const LineList<int>’ as ‘this’ argument of ‘void LineList<T>::Out(ostream&) [with T = int]’ discards qualifiers  

百思不得其解,于是百度,google吧。。

发现Stackoverflow上也有人有相同的问题


下面是他的问题:

For my compsci class, I am implementing a Stack template class, but have run into an odd error:

Stack.h: In member function ‘const T Stack<T>::top() const [with T = int]’:

Stack.cpp:10: error: passing ‘const Stack<int>’ as ‘this’ argument of ‘void Stack<T>::checkElements() [with T = int]’ discards qualifiers

Stack<T>::top() looks like this:

const T top() const {
    checkElements();
    return (const T)(first_->data);
}

Stack<T>::checkElements() looks like this:

void checkElements() {
    if (first_==NULL || size_==0)
        throw range_error("There are no elements in the stack.");
}

The stack uses linked nodes for storage, so first_ is a pointer to the first node.

Why am I getting this error?


正确答案是

Your checkElements() function is not marked as const so you can't call it on const qualified objects.

top(), however is const qualified so in top()this is a pointer to a const Stack (even if theStack instance on which top() was called happens to be non-const), so you can't callcheckElements() which always requires a non-const instance.

意思是说  在一个加了const限定符的成员函数中,不能够调用 非const成员函数。

因为如果调用了非const成员函数,就违反了const成员函数不改变对象的规定。

而error:...discards qualifiers 的意思就是缺少限定符

因此,他的解决办法很简单了 ,只要将checkElements()函数申明改为  checkElements()const就行了



再回到我的问题,

因为重载<<中有一个参数为 const LineList<T>&line

注意:const对象只能调用const成员函数;

所以调用line.Out(cout)的时候,Out()函数必须是const成员函数,因而出现了错误discards qualifiers

解决办法就是将Out(ostream& os)改为 Out(ostream& os)const 。




在编译C++程序的时候,如果遇到const的声明,经常会看到这样的提示:

aa.cpp:24: 错误:将 ‘const demo’ 作为 ‘void demo::show()’ 的 ‘this’ 实参时丢弃了类型

如果是英文,那么提示如下:

error: passing 'const demo' as 'this' argument of 'void demo::show()' discards qualifiers

 

原因如下:

定义const的类的时候,访问了非const的成员函数。

示例代码如下:

#include
using namespace std;
class demo {
private:
        int a;
        int b;
public:
        int geta() const { return a; }
        int getb() const { return b; }
        demo(void):a(0),b(0) { }
        demo(int x,int y):a(x),b(y) { }
       //
        // 如果show()函数没有声明const描述,那么在func函数中,是不能调用的
        // 因为func函数声明了const的变量
        //
        void show() const {
                printf("a=%d\n",a);
        }


};

void func(const demo& A)
{
        int f = A.geta();
        printf("f=%d\n",f);
        A.show();
}

int main(int argc,char *argv[])
{
        demo d(10,20);
        func(d);
        return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值