一个与map下标操作有关的编译错误(花了5分钟才找到原因)
来源:互联网 发布:python数据分析 视频 编辑:程序博客网 时间:2024/05/05 09:53
来, 看程序:
#include <iostream>#include <string>#include <map>using namespace std;class A{public:map<string, string> m_map;};void test(const A &a){string s = a.m_map["hello"];cout << s << endl;}int main(){A a;a.m_map["hello"] = "world";test(a);return 0;}g++编译错误如下:
main.cpp: In function ‘void test(const A&)’:
main.cpp:14: error: passing ‘const std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >’ as ‘this’ argument of ‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >]’ discards qualifiers
原因: 在test中, a的值不能变, 而a.m_map["hello"]是一个可读写操作, 所以有问题。 还是用const_iterator吧, 先查找"hello", 再访问it->second
- 一个与map下标操作有关的编译错误(花了5分钟才找到原因)
- 找到IAR 6.2/6.3编译以前项目的出一大堆错误的原因了
- STL map的insert操作与下标操作的区别
- 在const成员函数中使用下标[]操作map成员引发编译错误
- 一个和const有关的编译错误
- 编译libtorrent,花了两天时间,才找到合适版本并编译例子通过
- 与namespace有关的两个编译错误
- 找了半天才找到的代码
- const导致的编译错误, 浪费了10分钟
- makefile写错(多了.a)导致了cannot find -libtest.a------耗费10分钟才找出原因
- map下标操作
- eclipse中某工程的断点进不去问题,折腾了N个小时才找到原因,郁闷
- eclipse中某工程的断点进不去问题,折腾了N个小时才找到原因,郁闷
- 有关使用map的错误
- Rose中与Path Map有关的一个问题
- org.springframework.expression 这样才会输出详细错误,很容易找到错误原因
- 紧急求助!ARM-GCC对于函数指针调用的编译有错误?【已经找到原因】
- 开始看uc内核代码,不能编译,原因找到,估计的重装系统了,惨!
- 转--统计学习那些事
- Qt Quick里的图形效果:阴影(Drop Shadow)
- 第八周 OJ 小球自由下落
- 重启iscsi报19 - encountered non-retryable iSCSI login failure
- hive数据迁移
- 一个与map下标操作有关的编译错误(花了5分钟才找到原因)
- FragmentManager的使用注意事项
- ScrollView嵌套ListView(ViewGroup事件分发)
- 老板说做SEO只是发外链写软文的,怎么破?
- 欢迎使用CSDN-markdown编辑器
- 调一个cpld小板
- 关于Unity3D(4.7和5.3.4)与IOS原生Object-C交互,调用IOS原生UIImagePickerController的问题
- Eclipse 窗口,视图 说明
- Mysql -update - Error Code: 1175. You are using safe update mode and you tried to update a table wit