passing ‘const ’ as ‘this’ argument of ‘’ discards qualifiers 错误处理
示例程序:
#include <iostream>
#include <set>
using namespace std ;
class StudentT
{
public :
int id ; string name ;
public :
StudentT ( int _id , string _name ) : id ( _id ), name ( _name ) { }
int getId () { return id ; }
string getName () { return name ; }
};
inline bool operator < ( StudentT s1 , StudentT s2 ) { return s1 . getId () < s2 . getId (); }
int main ()
{
set < StudentT > st ;
StudentT s1 ( 0 , "Tom" );
StudentT s2 ( 1 , "Tim" );
st . insert ( s1 ); st . insert ( s2 );
set < StudentT > :: iterator itr ;
for ( itr = st . begin (); itr != st . end (); itr ++)
{
cout << itr -> getId () << " " << itr -> getName () << endl ;
}
return 0 ;
}
错误提示:
../main.cpp:35: error: passing 'const StudentT' as 'this' argument of 'int StudentT::getId()' discards qualifiers
../main.cpp:35: error: passing 'const StudentT' as 'this' argument of 'std::string StudentT::getName()' discards qualifiers
原因:
std::set的对象存储const StudentT 。 所以当您尝试调用getId() const对象的编译器检测到一个问题,即你调用一个const对象的非const成员函数这是不允许的,因为非const成员函数不作任何承诺,不修改对象,所以编译器将会使一个安全的假设getId()可能试图修改的对象,但同时,它也注意到,该对象是const,所以任何试图修改const对象应该是一个错误。 因此,编译器会生成错误消息。
解决方法:
解决方法很简单:函数的const:
int getId () const { return id ; }
string getName () const { return name ;}
passing ‘const ’ as ‘this’ argument of ‘’ discards qualifiers 错误处理的更多相关文章
- C++ error: passing 'const std::map<>]' discards qualifiers或pass-by-reference-to-const-map导致的“discards qualifiers”
产生问题的场景: int func(const map<int, string> &aMap) { string value = amap[0]; } 或者 int Test:: ...
- 关于c++ error : passing " "as" " discards qualifiers
http://www.cppblog.com/cppblogs/archive/2012/09/06/189749.html 今天写了一段小代码,本以为正确,但运行后,就somehow ”discar ...
- invalid initialization of non-const reference of type与discards qualifiers
参数传递 函数参数的传递是初始化语义:用调用者的实参去初始化函数的形参,如果参数是对象,需要调用该类的拷贝构造函数,如果没有显式定义的拷贝构造函数,则执行默认的按成员拷贝 ...
- 实参时丢弃了类型 discards qualifiers discards qualifiers问题
百思不得其解,于是百度,google吧.. 发现Stackoverflow上也有人有相同的问题 下面是他的问题: For my compsci class, I am implementing a S ...
- C++ 编译报错discards qualifiers [-fpermissive]
声明了一个类 class Card { public: Card(const string&); int m_value; char m_suit; private: const static ...
- c++ 异常 discards qualifiers 丢弃
src/feedbackservice.cpp:76: error: passing `const ps::spider::urlreceiver::entry::ConfigManager' as ...
- jni编译non-numeric second argument to `wordlist' function错误
在jni编译过程中,遇到non-numeric second argument to `wordlist' function错误,个人遇到这个错误的原因,是因为从windows中拷贝了Android. ...
- PHP:Invalid argument supplied for foreach()错误原因及解决办法
在php中使用foreach循环遍历时报Invalid argument supplied for foreach()错误,是因为循环的数据不是一个有效的数组. 因此我们只要在foreach之前判断一 ...
- CTC安装及其错误解决办法:binding.cpp:92:49: error: cannot convert ‘THCudaTensor*’ to ‘const THFloatTensor*’ for argument ‘1’ to ‘int64_t THFloatTensor_size(const THFloatTensor*, int)’
CTC安装: 1. 在终端执行命令:git clone https://github.com/SeanNaren/warp-c) (效果如下图,大家不用管我前面括号的内容,那是我打开的虚拟环境) 2. ...
随机推荐
- (C#基础) byte[] 之初始化, 赋值,转换。(转)
byte[] 之初始化赋值 用for loop 赋值当然是最基本的方法,不过在C#里面还有其他的便捷方法. 1. 创建一个长度为10的byte数组,并且其中每个byte的值为0. byte[] myB ...
- 认识B/S架构
Browser/Server即浏览器/服务器模式. Web浏览器主要功能 1. 申请服务,包括服务器的Ip地址和文件 2. 从服务器上下载 3. 解析下载的文件 4. 用过http协议进行通信 Web ...
- Jmeter之JDBC请求(四)
我们常用的Jmeter中的功能又HTTP请求.JDBC Request.SOAP/XML -RPC Request,这3个请求, 现在就为大家介绍下 什么是JDBC请求 首先,大家右键点击“测试计划” ...
- lr_think_time参数化
深圳湖北籍软件测试群 275212937
- Linux解压文件
zip: 解压:unzip filename 解压到tmp文件夹:unzip filename.zip -d /tmp 查看压缩文件而不解压:unzip filename.zip -v tar.gz: ...
- super一些要点
package o6; class Grandparent { public Grandparent() { System.out.println("GrandParent Created. ...
- jQuery-认识JQuery,jQuery选择器
认识JQuery: 1.window.onload与$(document).ready()的区别 window.onload $(document).ready() 执行时机 必须等待网页中的所有内容 ...
- HDU5008 Boring String Problem(后缀数组 + 二分 + 线段树)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5008 Description In this problem, you are given ...
- HD1847-(博弈论??)
Good Luck in CET-4 Everybody! Problem Description 大学英语四级考试就要来临了,你是不是在紧张的复习?也许紧张得连短学期的ACM都没工夫练习了,反正我知 ...
- Python version 2.7 required, which was not found in the registry
http://blog.csdn.net/zdnlp/article/details/12171687