簡體   English   中英

在std :: list上編譯錯誤

[英]Compile error on std::list

嘗試使用命令g++ -std=c++11 te1.cc在我的ubuntu 12.04上編譯以下代碼片段(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] 

順便說一句, 最小,完整和可驗證的示例如下:

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

    l.insert(it, 5);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM