2011-10-03 5 views
3

템플릿을 만들려고했지만 GCC4에는 오류가 있지만 VS2008에는 오류가 있습니다.템플릿의 오류 : Iterator가

#pragma once 
#ifndef _ZELESTE_ZEL_GPX_GENERALMANAGER_H_ 
#define _ZELESTE_ZEL_GPX_GENERALMANAGER_H_ 

#include "../internal/cuCoreLib.h" 
#include "boost/functional/hash.hpp" 
#include "boost/ptr_container/ptr_map.hpp" 

namespace z3d{ 
    namespace core{ 
     template<class Key, class Value> 
     class cuManager 
     { 
      boost::ptr_map<Key, Value> m_ItemMap; 

     public: 
      /** 
      * Default constructor 
      */ 
      cuManager(void){} 

      /** 
      * Get a vector that contain the keys of the elements contained in the manager. 
      * @return an const std::vector of type Key 
      */ 
      const std::vector<Key> getKeys() 
      { 
        boost::ptr_map<Key,Value>::iterator itr = m_ItemMap.begin(); 
        std::vector<Key> v; 
        while(itr != m_ItemMap.end()) 
        { 
          v.push_back(itr->first); 
          ++itr; 
        } 
        return v; 
      } 

      } 
     }; 
    }; 

이 (클래스의 모든 메소드가 같은 반복자 실패) 컴파일에 실패 방법 중 하나입니다 :이 실패 코드입니다. 이 코드는 GCC에서 비주얼 스튜디오하지만 컴파일로 잘 작동이 오류를 반환 :

/home/dev001/desarrollo/code/lpgameengine/LPGameEngine/src/core/datatypes/cuManager.h: In member function ‘const std::vector<_Tp, std::allocator<_CharT> > z3d::core::cuManager<Key, Value>::getKeys()’: 
/home/dev001/desarrollo/code/lpgameengine/LPGameEngine/src/core/datatypes/cuManager.h:28: error: expected ‘;’ before ‘itr’ 
/home/dev001/desarrollo/code/lpgameengine/LPGameEngine/src/core/datatypes/cuManager.h:30: error: ‘itr’ was not declared in this scope 

어떤 도움을 환영 할 것이다

답변

6

은 다음과 같이 선언 :

typename boost::ptr_map<Key,Value>::iterator itr = m_ItemMap.begin(); 
^^^^^^^^ 

포인트는 boost::ptr_map<Key,Value>::iterator이 인이다 종속 이름이므로 변수 이름이나 템플릿 이름이 아닌 형식 이름으로 지정해야합니다.

+0

그리고 완료하십시오 : Visual Studio는 템플릿과 관련하여 완전히 손상되어 유효하지 않은 코드를 받아들입니다. gcc와 Clang의 이름은 룩 - 업 (look-up)을 할 때 커버해야 할 부분이 있지만, 덜 깨진 것입니다 : x –

+1

@MatthieuM .: Thanks! 필자는 MSVC에 대한 경험이 거의 없기 때문에 (MSVCExpress는 2GB의 소중한 C : 파티션을 재배치 할 필요가 없기 때문에!), 공통 컴파일러의 특이성에 대해 항상 배워서 기쁩니다. 이름 검색과 관련하여 GCC는 어떻게 고장 났는가? –

+1

[이름 조회에 대한 질문] (http://stackoverflow.com/questions/7630806/the-actual-result-of-name-resolution-in-the-class-template-is-different-from-the)을 참조하십시오. , gcc와 clang은 Standard에서 직접 예제를 잘못 컴파일합니다 : x –