2013-06-20 5 views
3
#include <iostream> 
#include <map> 
#include <string> 

using namespace std; 

template <class T> 
class Counter 
{ 
    public: 
     Counter() 
     { 
      totalCount  
     } 

     ~Counter() 
     { 
     } 

     bool containsKey(T key) 
     { 
      map<T, double>::iterator it = counter.find(T); 
      if (it == counter.end()) return false; 
      return true; 
     } 

    private: 
     map<T, double> counter; 
     double totalCount; 
}; 

int main() 
{ 
    Counter<string> table; 
    return 0; 
} 

이 코드는 컴파일되지 않으며 오류의 원인을 파악할 수 없습니다. 어떤 도움을 주시면 감사하겠습니다. 감사!종속 범위 오류 stl

는 오류가

error: need ‘typename’ before ‘std::map<T, double>::iterator’ because ‘std::map<T, double>’ is a dependent scope 
+6

에 나는 오류가 당신에게 아주 좋은 힌트 –

+4

'하면 것은 (...) false를 돌려주고있다 생각; 그렇지 않으면 true를 반환하고, '는 반 패턴입니다. 대신'return not ...;'를 써라. –

+0

그 대회를 지적 해 주셔서 감사합니다. 그것을 사용하게됩니다 ... – shashydhar

답변

6

컴파일러는 T가 템플릿 선언에서 유형 (유형 이름)의 이름임을 알고있다

g++ counter.cpp 

를 컴파일 할 cmd를하지만, 경우 표준을 알 수 없습니다 : : map :: iterator는 타입이나 다른 것입니다. 따라서 컴파일러에서이 문 앞에 'typename'을 추가하여 컴파일러에게 유형의 이름을 지정해야한다고 말합니다. 요약으로

: 변화

map<T, double>::iterator it = counter.find(T); 

typename map<T, double>::iterator it = counter.find(T);