2013-10-15 1 views
1

다음 코드 (g ++ 4.7.1 사용)를 컴파일 할 수없는 이유는 무엇입니까? 은 "분명()"메소드를 호출 할 수없는 문제를 검사하는 동안 나는이 문제를 가로 질러 온템플릿 클래스 멤버 메서드 내에서 자동 람다 : 형식 이름을 지정하지 않습니다.

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

:

#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; 
} 

나는 다음과 같은 오류가 발생합니다. 왜 'CONST는'여기

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

을 포함한다 :

추가 '-std = C++ 11'정말 내 진짜 문제에 도달 할 수 있습니다?

+5

C++ 11을 사용할 수 있습니까? – 0x499602D2

답변

2

GCC 4.7의 버그입니다. 4.8로 업그레이드하면 문제가 해결됩니다. 명백하게 GCC 4.7은 thisconst으로 잘못 캡처했습니다.

+0

'this'는 포인터이기 때문에 객체를 가리키는 한 const가 될 필요가 없습니다 변경 될 수 있습니다 –

+0

@Heiko 예. 업데이트 참조 ;-) –

+0

Thx. 나는 잠을 자고 직장에서 이것을 시도 할 것이다. –

2

컴파일러에서 C++ 11을 사용할 수 없기 때문입니다. GCC에 --std=c++11 플래그를 사용해보십시오. 그럼 컴파일이 잘되어야합니다.

+0

큰 응용 프로그램에서 문제를 추출하고 MWE를 설정 한 결과입니다. :-( –

+0

문제가'--std = C++ 11' 이상인 것 같아요, Konard의 대답을보세요 – deepmax

0

-std=c++11으로 시도해보십시오. 안타깝게도, 현재 컴파일러는 여전히 10 년 이상 된 표준을 기본으로합니다.

관련 문제