discard qualifiers的错误
来源:互联网 发布:网易uu加速器 for mac 编辑:程序博客网 时间:2024/04/29 01:50
写代码时,没有注意到或者说不清楚的问题,discard qualifiers的问题,网上有人碰到此类问题并附介绍:
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就行了
- discard qualifiers的错误
- c++ XXXXXX discards qualifiers [-fpermissive] 错误原因
- mysql 错误Please DISCARD the tablespace before IMPORT解决方案
- Netty5 最简单的例子 Writing a Discard Server
- Xcode误点'Discard all changes'的恢复方法
- libevent discard
- dicards qualifiers
- 对于一般的数组类型,简单使用TYPE_MAIN_VARIANT无法将所有的qualifiers去掉?
- linux文件系统如何使用emmc的擦除功能(Erase Trim Discard Sanitize)达到最优performance
- emmc FITRIM/discard命令在android系统中提升write performance的运用
- emmc FITRIM/discard命令在android系统中提升write performance的运用
- emmc FITRIM/discard命令在android系统中提升write performance的运用
- 关于passing ‘const *’ as ‘this’ argument of ‘*’ discards qualifiers的问题
- error: passing xxx as 'this' argument of xxx discards qualifiers的解决办法
- EraseTrim Discard Sanitize详解
- Netty -- Discard Server
- Cg shader using discard
- Erase/Trim/Discard/Sanitize
- AsyncTask的用法
- Android的五个进程等级
- 用python的BeautifulSoup分析html
- poj 2115-扩展欧几里得。
- 用python绘制漂亮的图形
- discard qualifiers的错误
- innerHTML 属性使用方法
- 腾讯微博客户端开发---若水(第五集、第六集)
- 《万历十五年》 黄仁宇
- C语言 第一章 程序设计的基本概念
- CTBS问题百科
- 关于链接过程中出现的“找到一个或多个多重定义的符号”和“无法解析的外部符号”错误的探究
- SharePoint Solutions Deployment-PowerShell
- 日语:假定形、可能形、被动形、使役形、使役被动形