Question

Why am I not able to compile the following code (with g++ 4.7.1)?

#include <set>

template<typename T>
class Problem
{
    public:
        void f();

        std::set<int> dummyvalue;
};

template<typename T>
void Problem<T>::f()
{
    auto mytestlambda = [this](){
        dummyvalue.clear();
    };
}

int main()
{
    return 0;
}

I get the following error:

main.cpp: In member function 'void Problem<T>::f()':
main.cpp:17:10: error: 'mytestlambda' does not name a type

I have come across this problem while examining the problem not being able to call the "clear()" method.

Adding '-std=c++11' really lets me reach my real problem:

main.cpp: In lambda function:
main.cpp:18:26: error: no matching function for call to 'std::set<int>::clear() const'
main.cpp:18:26: note: candidate is:
In file included from /usr/include/c++/4.7/set:61:0,
                 from main.cpp:1:
/usr/include/c++/4.7/bits/stl_set.h:580:7: note: void std::set<_Key, _Compare, _Alloc>::clear() [with _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>] <near match>
/usr/include/c++/4.7/bits/stl_set.h:580:7: note:   no known conversion for implicit 'this' parameter from 'const std::set<int>*' to 'std::set<int>*'

Why is 'const' here involved?

Was it helpful?

Solution

It’s a bug in GCC 4.7. Upgrading to 4.8 fixes it.

Apparently GCC 4.7 erroneously captured this as const.

OTHER TIPS

It is because you don't have C++11 enabled in the compiler. Try to use --std=c++11 flag for GCC. Then it should compile right.

try compiling with -std=c++11. Sadly, current compilers still default to a >10 year old standard.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top