2

次のコードを (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;
}

次のエラーが表示されます。

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

「clear()」メソッドを呼び出せない問題を調べているときに、この問題に遭遇しました。

「-std=c++11」を追加すると、実際の問題に到達できます。

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>*'

ここで「const」が関係しているのはなぜですか?

4

3 に答える 3

0

でコンパイルしてみてください-std=c++11。悲しいことに、現在のコンパイラは依然として 10 年以上前の標準にデフォルト設定されています。

于 2013-10-15T21:40:28.730 に答える