gpt4 book ai didi

c++ - std::list 上的编译错误

转载 作者:行者123 更新时间:2023-11-28 06:16:58 24 4
gpt4 key购买 nike

尝试在我的 ubuntu 12.04 上编译以下代码片段,使用命令 g++ -std=c++11 te1.cc,(g++ 版本 4.7.3)

typedef unsigned int uint;
typedef std::unordered_map< uint, uint > imap;

void printTop (imap &m, int n=3) {
std::list<uint> l;
uint tmp;
int size;
for (auto kv: m) {
size = l.size();
if (size == 0) {
l.push_front(kv.first);
continue;
}
std::list<uint>::const_iterator it=l.begin();
while (it != l.end()) {
tmp = *it;
if (kv.second >= m[tmp]) {
tmp = kv.first;
l.insert(it, tmp);
}
}
if (l.size() > n) { l.pop_back();}
}

}

出现错误:

error: no matching function for call to ‘std::list<unsigned int>::insert(std::list<unsigned int>::const_iterator&, uint&)’
te1.cc:29:21: note: candidates are:
In file included from /usr/include/c++/4.7/list:65:0,
from te1.cc:8:
/usr/include/c++/4.7/bits/list.tcc:99:5: note: std::list<_Tp, _Alloc>::iterator std::list<_Tp, _Alloc>::insert(std::list<_Tp, _Alloc>::iterator, const value_type&) [with _Tp = unsigned int; _Alloc = std::allocator<unsigned int>; std::list<_Tp, _Alloc>::iterator = std::_List_iterator<unsigned int>; std::list<_Tp, _Alloc>::value_type = unsigned int]
/usr/include/c++/4.7/bits/list.tcc:99:5: note: no known conversion for argument 1 from ‘std::list<unsigned int>::const_iterator {aka std::_List_const_iterator<unsigned int>}’ to ‘std::list<unsigned int>::iterator {aka std::_List_iterator<unsigned int>}’
In file included from /usr/include/c++/4.7/list:64:0,

有什么想法吗?

谢谢。

最佳答案

您的编译器未完全实现 C++11。从 GCC 4.9.0 开始,您的代码将可以正确编译。

[5:25pm][wlynch@apple /tmp] /opt/gcc/4.9.0/bin/g++ -std=c++11 -c red.cc
[5:25pm][wlynch@apple /tmp]

顺便说一句,Minimal, Complete, and Verifiable example因为这将是:

int main() {
std::list<unsigned int> l;
std::list<unsigned int>::const_iterator it = l.begin();

l.insert(it, 5);
}

关于c++ - std::list 上的编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30064211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com