C++ : 出错解释 base operand of '->' has non-pointer type 'std::pair<int, int>'
2017-02-21 22:52
5836 查看
C++ ERROR : base operand of ‘->’ has non-pointer type ‘std::pair< int, int>’ 的解释
问题描述
出错代码(截取部分):
list<pair<int,int>> cachelist; unordered_map<int,list<pair<int,int>>::iterator> map; void put(int key, int value) { auto it = map.find(key); if(it != map.end()){ touch(it->second); it->second->second = value; // ① } else if(map.size() < cap){ cachelist.push_front(make_pair(key,value)); map[key]=cachelist.begin(); } else{ auto it = cachelist.back();// ② map.erase(it->first); // 出错位置~~!! cachelist.pop_back(); cachelist.push_front(make_pair(key,value)); map[key]=cachelist.begin(); } }
报错内容:
Line xx: base operand of '->' has non-pointer type ' std::pair <int, int>'
分析与解决
首先unordered_map的
erase()函数的参数可以是键值,可以是迭代器,也可以是迭代器区间,那么肯定不是
erase()的问题;
然后报错提示告诉我们
pair<int,int>不能用
->符号,那就奇怪了,位置①我们不是也用了
it->second->second吗?①处的
it是
unordered_map的
iterator,
it->second是
list<pair<int,int>>的
iterator,所以
it->second->second是
pair的第二值,好像没什么不对??
到这里可能你跟我一样,发现问题了,②处的
it并不是
list<pair<int,int>>的迭代器,而是
cachelist的最后一个元素节点的地址,auto实际上应该是
pair<int,int> &,而
pair<int,int>是不认识
->符号的,所以出错位置的应该把
->改成
.,即:
map.erase(it.first);
参考链接
http://stackoverflow.com/questions/21058894/error-base-operand-of-has-non-pointer-type-stdpairint-int相关文章推荐
- base operand of '->' has non-pointer type 'JNIEnv {aka _JNIEnv}'和Method 'GetStringUTFChars' could no
- 第一次做树的题,并且用到了vector<pair<int,int> >,收获很大
- C++中vector<int>和vector<int*>的区别
- set<pair<int,int> >的用法
- [C++]pair<int,int>与make_pair<int,int>
- base operand of '->' has non-pointer type 'JNIEnv {aka _JNIEnv}'和Method 'GetStringUTFChars' could no
- 不允许使用“{...}”对”std::vector<int,std::allocator<int> >“类型的对象进行初始化
- C++中map<int,vector<int>>的初始化和访问方法
- JNI: error: base operand of ‘->’ has non-pointer type ‘JNIEnv’
- JNI错误之:error: base operand of '->' has non-pointer type '_JNIEnv'
- error: base operand of ‘->’ has non-pointer type ‘JNIEnv’
- 2,3 c++中 #include< >头文件 和 命名空间 Using namespace std的解释和区别
- c++中vector<int>和vector<int*>的用法比较
- C++中的&nbsp;pair&lt;map&lt;string,&nbsp;int&gt;::i…
- vector<pair<int,int> >的插入排序,字符串中子字符串个数查找
- UESTC 1603 BanG Dreamer 小根堆、贪心+set<pair<int, int>>+low_bound
- C++ std::pair<>
- priority_queue<int,vector<int>,greater<int>>优先队列 按照由小到大顺序
- C++用map记录字符串,怎么用map<string,int>才会不超时
- C++ vector<int>::size_type的使用