packetie
packetie

Reputation: 5069

Compile error on std::list

Tried to compile the following code snippet on my ubuntu 12.04, using command g++ -std=c++11 te1.cc, (g++ version 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();}
    }

}   

Got error:

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,

Any ideas?

Thanks.

Upvotes: 0

Views: 571

Answers (1)

Bill Lynch
Bill Lynch

Reputation: 82026

Your compiler doesn't fully implement C++11. Starting with GCC 4.9.0, your code will compile correctly.

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

And by the way, the Minimal, Complete, and Verifiable example for this would be:

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

    l.insert(it, 5);
}

Upvotes: 1

Related Questions