2013-12-06 2 views
1

잠금 해제 맵을 구현하려고하는데 문제가 발생했습니다. 내 반복자 클래스에서클래스에 적합한 연산자가 없습니다. ->

가 나는 operator-> 자유롭게 firstsecond를 얻기 위해 구현 :

template <class _Kty, class _Ty> 
struct _t_iterator_return 
{ 
    _Kty& first; // key 
    _Ty& second; // val 

    _t_iterator_return(_Kty& k, _Ty& v) : first(k), second(v) {} 
}; 

그것이 표준이되지 않는 것 같습니다 :입니다

_t_iterator_return<_Kty, _Ty> operator->() 
{ 
    _t_iterator_return<_Kty, _Ty> tmp((int&)Cur->getKey(), Cur->getValue()); 
    return tmp; 
} 

? 하지만 내 main()이 :이 코드 그럼에도 불구하고 error C2819: type 'lightforce::core::LFMap<_Kty,_Ty>::_t_iterator_return<_Kty,_Ty>' does not have an overloaded member 'operator ->'

을 :

for(LFMap<int, stringc>::iterator it = m.begin(); !it.end(); ++it) 
    fprintf(mres, "K: %7d  V: %s\n", it->first, it->second.c_str()); 

은 컴파일 타임 오류가 발생한다

for(LFMap<int, stringc>::iterator it = m.begin(); !it.end(); ++it) 
     fprintf(mres, "K: %7d  V: %s\n", it.operator->().first, it.operator->().second.c_str()); 

성공적으로 컴파일 잘 작동되었습니다! 왜 ->이 작동하지 않지만 operator->은 작동합니까?

답변

3

C++ 11 §13.5.6 [over.ref] 파라 1 주 : 당신이에서 반환 유형부터

... An expression x->m is interpreted as (x.operator->())->m for a class object x of type T if T::operator->() exists and if the operator is selected as the best match function by the overload resolution mechanism (13.3).

당신의 operator->-_t_iterator_return<_Kty, _Ty> - operator ->가이 역 참조 할 수 없습니다. 나는 그것을 추가이있어

_t_iterator_return* operator->() 
{ 
    return this; 
} 
+0

: '오류 C2818 :'-> 운영자 '유형'lightforce :: 핵심 통해 반복적 인 과부하의 응용 프로그램을

operator -> 단순히 this 반환 _t_iterator_return에 추가 :: LFMap <_Kty,_Ty> :: _ t_iterator_return <_Kty,_Ty> ' – Netherwire

+0

@RomanChehowsky [clang] (http://coliru.stacked-crooked.com/a/eebb74b503f2b09b) 및 [g ++] (http : //coliru.stacked -crooked.com/a/317a4c9464745e36). MSVS 버그 일 수 있습니다. – Casey

+0

기다릴 필요가 없습니다. 하지만'_t_iterator_return <_Kty, _Ty> * 연산자 -> {return this;}','_t_iterator_return이 아닌 <_Kty, _Ty> & 연산자 -> {return * this;}'내가 시도한 :) Thx 많이! – Netherwire

관련 문제