2011-11-13 6 views
1

내가 템플릿을 사용하여 C++ 표준 라이브러리의 벡터 클래스를 캡슐화하기 위해 노력하고있어하지만 난이 오류가 계속이C++ 템플릿 래퍼 클래스 : 벡터

SceneVector.h: In member function ‘void scenegraph::SceneVector<V>::print()’: 
SceneVector.h:40: error: expected ‘;’ before ‘it’ 
SceneVector.h:40: error: ‘it’ was not declared in this scope 

내가 만들어 관리했습니다 코드는

입니다
#include <map> 
#include <vector> 
#include <iostream> 

namespace scenegraph 
{ 
    template <class V> class SceneVector 
    { 
     typedef std::vector<V> Vector; 
     Vector vector; 

     public: 
      SceneVector(); 
      void insert(const V value); 
      void print(); 
    }; 

    template <class V> SceneVector<V>::SceneVector() 
    { 
     vector.clear(); 
    } 

    template <class V> void SceneVector<V>::insert(const V value) 
    { 
     vector.push_back(value); 
    } 

     template <class V> void SceneVector<V>::print() 
    { 
     for(Vector::iterator it = vector.begin(); it != vector.end(); ++it) 
     { 
      std::cout << "[" << (*it) << "] " << std::endl; 
     } 
     std::cout << std::endl; 
    } 
} 

누구든지 나를 교정 할 수 있습니까? 내가 C++ 초보자이기 때문에 답이 매우 사소할 수도 있습니다.

+2

또 하나 ... 'typename'이 필요합니다. http://stackoverflow.com/q/1123080/51831 – jpalecek

+1

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.21 – ephemient

답변

3

템플릿 매개 변수에 종속 된 형식에 액세스 할 때는 형식이 유형이라는 것을 구문 분석기에 알리기 위해 typename 앞에 붙여야합니다.

for(typename Vector::iterator it = vector.begin(); it != vector.end(); ++it) 

Vector :: iterator는 컴파일 타임 상수가 될 수 있으며 컴파일러는 인스턴스화 시간까지 알 수 없습니다. 그래서 명시 적으로 말해야합니다.

+0

정확합니다. 그건 그렇고 내가 여기서 얻으려고하는 것은 내가 삽입 된지도를 삽입 할 수있는 데이터 구조이다. 나는 삽입 순서를 유지하기위한 벡터와 key to value 관계를 유지하는지도를 사용한다. 올바른 방향으로 가고 있습니까? –

+1

@ JorgeSilva : Boost.MultiIndex를 사용할 수 있습니다. – thiton

+0

글쎄, 그럴 수 없어. 학자 프로젝트이고 Boost는 허용되지 않습니다 ... –

3

당신이 일반

std::vector<V>::iterator 

은 의존하는 이름이 꽤 명백하고, 그 반복자는 유형 표시하기 위해 typename이 필요하다는 것이 있다면.

typedef를 사용하면 실제로 변경되지 않습니다.