2011-05-14 4 views
1

이상한 오류가 발생하지만 난 말을 컴파일 오류 "예상, 그것은 전에"난 그냥지도 반복자를 선언하려고

는 내가 포함되지 않았기 때문에 그것이 믿는다 전체 std 네임 스페이스 (네임 스페이스 std를 사용하여)하지만 의도적으로 모든 것을 포함하고 싶지 않습니다.

내 코드 :

#include <map> 
#include <string> 

template <class Object> 
class Cont 
{ 
    public: 
     Cont() {} 
     Object* get(unsigned int nID) 
     { 
      std::map <unsigned int, Object*>::iterator it = m.begin(); // error here "expected ; before it" what is this error? 

      for (; it != m.end(); it++) 
      { 
       if ((*it).second->ID == nID) { return (*it).second; } 
      } 

      return NULL; 
     } 

     std::map <unsigned int, Object*> m; 
}; 

나는이 aswell을 시도했지만 작동하지 않습니다

std::map <unsigned int, Object*>::std::iterator it = m.begin(); 
+0

이 템플릿 클래스보다 먼저 'Object' 클래스를 전달 하시겠습니까? 그렇지 않다면 그 원인이 될 수 있습니다. –

+0

세부 정보 : http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-template-and-typename-on-dependent-names/613132#613132 –

답변

10

템플릿 인수를 사용하고 있기 때문에 착각하지 않는다면 이터레이터 선언 앞에 typename을 접두어로 사용해야합니다.

typename std::map <unsigned int, Object*>::iterator it = m.begin(); 
+2

이 [discussion] (http://pages.cs.wisc.edu/~driscoll/typename.html)이 typename을 찾는데 도움이된다. 왜 그리고 언제 typename 키워드가 필요한지 이해하는 것. – rlduffy

0

당신은 당신이 사용중인 컴파일러 말을하지 않지만 단지 절단 및 이 파일을 새 파일에 붙여 넣으면 VS2010에서 정상적으로 컴파일됩니다. using namespace std; 확실히 ....

(또 다른 std :: before iterator를 넣는 주름은 창조적 이었지만 정확하지 않습니다. :-)지도 클래스 템플릿이 네임 스페이스 std ::에 있고 이터레이터는 맵 템플릿 내에 중첩 된 유형입니다.

+0

나는 그 문제를 발견했다. 나는 typename std :: map을 사용할 필요가있다. devC++의 컴파일러를 사용하고있다. – user593747

1

컴파일러 및 플래그 설정은 무엇입니까? 나는 이것을 만들 수 있었다.

// test.cpp : Defines the entry point for the console application. 
// 

#include "stdafx.h" 

#include <map> 
#include <string> 

class Foo 
{ 
public: 
    int ID; 
}; 

template <class Object> class Cont 
{ 
    public: 
     Cont() {} 
     Object* get(unsigned int nID) 
     { 
      std::map <unsigned int, Object*>::iterator it = m.begin(); // error here "expected ; before it" what is this error? 

      for (; it != m.end(); it++) 
      { 
       if ((*it).second->ID == nID) { return (*it).second; } 
      } 

      return NULL; 
     } 

     std::map <unsigned int, Object*> m; 
}; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    Cont<Foo> c; 
    c.get(2); 
    return 0; 
} 
+2

가끔 ** 어쨌든 알아낼 수있는 MS 컴파일러를 사용하고 있습니다. 표준에 따르면 아직 누락 된 키워드에 대해 불만을 제기해야합니다. –

+0

예 - 나는 이것에 의해 과거에 물렸지만 gcc를 최근에 사용하지 않고 잊어 버렸습니다. 코드 및 코드를 사용하여 오류 및 해결 방법을 확인했습니다. http://codepad.org/GSCVOVj1 - typename 키워드가 유형 대 값의 사용을 모호하게 설명한다는 SO 스레드가 있습니다. http://stackoverflow.com/questions/1215055/왜 - typedef-in-this-template-necessary-use-this-template-necessary - 이것으로부터 얻는 가장 유용한 테이크 아웃은 이것에 대한 typedef를 사용하는 또 다른 이점 (가독성을 제외하고)이다. 이터레이터. – holtavolt

관련 문제