2009-10-13 3 views
5

std :: set 주위에 템플릿 래퍼를 만들고 있습니다. Begin() 함수 선언에 오류가 발생하는 이유는 무엇입니까?std :: set를 반환하는 중 오류가 발생했습니다. :: 템플릿의 반복자

template <class T> 
class CSafeSet 
{ 
    public: 
     CSafeSet(); 
     ~CSafeSet(); 

     std::set<T>::iterator Begin(); 

    private: 
     std::set<T> _Set; 
}; 

오류 형식 '표준 : : 설정, 표준 : : 할당은 < _CharT>>' 'CSafeSet'

+3

참고 사항 : 식별자에서 밑줄을 사용하는 방법에 대해 읽으십시오. http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-ac-identifier/228797#228797 –

+1

짧은 버전 : "식별자 이름을 밑줄로 시작하지 마십시오. " – jalf

+0

긴 버전 : 대문자가 뒤에 오는 밑줄은 컴파일러 용으로 예약되어 있습니다. 더블 언더 스코어 다음에 컴파일러를 위해 예약 된 것도 있습니다. – GManNickG

답변

17

유형에서 파생되지 typename을 시도해보십시오

template <class T> 
class CSafeSet 
{ 
    public: 
     CSafeSet(); 
     ~CSafeSet(); 

     typename std::set<T>::iterator Begin(); 

    private: 
     std::set<T> _Set; 
}; 

당신은 유형 이름이 필요합니다 거기에 템플릿 T에 의존하기 때문에. 코드 위의 링크에 자세한 정보가 있습니다. 보조 노트에

template <class T> 
class CSafeSet 
{ 
    public: 
     typedef T value_type; 
     typedef std::set<value_type> container_type; 
     typedef typename container_type::iterator iterator_type; 
     typedef typename container_type::const_iterator const_iterator_type; 

     CSafeSet(); 
     ~CSafeSet(); 

     iterator_type Begin(); 

    private: 
     container_type _Set; 
}; 

당신이 CSafeSet 사용자 정의 비교자를 사용하여 의미하는 set could 같은 일을 할 수 있도록 할 필요가 완료하려는 경우 : 당신은 타입 정의의를 사용하는 경우 부지의이 물건은 쉽게 만들어 및 할당 : 당신이 클래스 래퍼를 만들려고하는 경우에

template <class T, class Compare = std::less<T>, class Allocator = std::allocator<T> > 
class CSafeSet 
{ 
    public: 
     typedef T value_type; 
     typedef Compare compare_type; 
     typedef Allocator allocator_type; 

     typedef std::set<value_type, compare_type, allocator_type> container_type; 
     typedef typename container_type::iterator iterator_type; 
     typedef typename container_type::const_iterator const_iterator_type; 

     // ... 
} 

그리고 조언의 마지막 비트, 클래스가 어디에서 왔는지와 같은 이름 지정 규칙을 따라야하려고합니다. 즉, Begin()begin()이어야합니다. (개인적으로 클래스 이름 앞에 C가 이상하다고 생각하지만 그 사람은 당신에게 달려 있습니다.)

+1

CSafeSet 클래스 템플릿에 오류가 있습니다. 또한 iterators 유형을 정의하려면 typename이 필요합니다. 또한 iterators 유형도 종속되어 있기 때문입니다. 예 : typedef ** typename ** container_type :: iterator iterator_type; 이 typename이 없으면 표준 준수 컴파일러에서 컴파일하면 안됩니다. Visual Studio (2008 년 이전 버전, 수정 된 것 같음)에서이 도구를 사용해 보았습니다. 실수로 typename없이 이러한 줄을 허용했습니다. –

+0

나는 전혀 시도하지 않았다. 그 점을 지적 해 주셔서 고마워, 나는 어리 석다 (매우 공통적 인) :) – GManNickG

+0

고마워, GMan. – jackhab

관련 문제