2011-03-10 5 views
4

멀티 인텍스 컨테이너가 포함 된 일반 클래스를 저장소로 만들어야합니다. 컴파일 할 때 n 번째 인덱스 뷰를 정의한 곳에서 아래와 같은 오류가 발생합니다.다중 인덱스 컨테이너를 높이기위한 템플릿 매개 변수

오류 : 템플릿이 아닌 'nth_index가'템플릿으로 사용


/** 
* connection manager 
*/

template < typename T, typename C > class conn_mgr: boost::noncopyable { public: /** * connection ptr */ typedef boost::shared_ptr conn_ptr_t;
/** * connection table type * It's a multi index container */ typedef boost::multi_index::multi_index_container < conn_ptr_t, boost::multi_index::indexed_by < //sequenced < >, boost::multi_index::hashed_unique < BOOST_MULTI_INDEX_CONST_MEM_FUN(T, std::string, T::id) >, boost::multi_index::hashed_non_unique < BOOST_MULTI_INDEX_CONST_MEM_FUN(T, std::string, T::type)>, boost::multi_index::hashed_non_unique < boost::multi_index::composite_key < conn_ptr_t, BOOST_MULTI_INDEX_CONST_MEM_FUN(T, std::string, T::id), BOOST_MULTI_INDEX_CONST_MEM_FUN(T, std::string, T::type) > > > > conn_table_t;

//typedef for ConnectionIdView 
typedef conn_table_t::nth_index<0>::type conn_table_by_id_type; 

typedef conn_table_t::nth_index<1>::type conn_table_by_type; 

typedef conn_table_t::nth_index<2>::type conn_table_by_id_type; 

개인 : conn_table_t의 conn_table_; }; 대신 중첩 된 형식 정의에 대한

and here how I am using in main.

int main(int argc, char** argv) { typedef conn_mgr < smpp_conn, smpp_config > smpp_conn_mgr_t; smpp_conn_mgr_t conn_mgr; }

+1

템플릿을 typedef 할 수 없습니다. 이 줄은 유효하지 않습니다 :'typedef boost :: shared_ptr conn_ptr_t; ' –

답변

11

사용이 구문 :

typedef typename conn_table_t::template nth_index<0>::type conn_table_by_id_type; 

typename 키워드는 컴파일러 수 있도록 규정 여기에 사용됩니다 conn_table_t::template nth_index<0>::type이 유형 것을 알고있다. typename의 특수 용도는 템플릿 내에서만 필요합니다.

여기에서 template 키워드는 다른 이름에서 distingush 회원 템플릿의 한정자로 사용됩니다.


또한,이 라인은 무효입니다 : 당신은 템플릿 형식 정의를 할 수

typedef boost::shared_ptr conn_ptr_t; 

. 형식을 typedef 할 수 있습니다. 아마 당신은 쓰는 의미 :

typedef typename boost::shared_ptr<T> conn_ptr_t; 

마지막 오류 : 문서화, conn_table_by_id_type


대신 BOOST_MULTI_INDEX_CONST_MEM_FUN(T, std::string, T::id)BOOST_MULTI_INDEX_CONST_MEM_FUN(T, std::string, id)를 사용한다 : 당신은이 구조체에는 동일한 이름을 부여하려고하고 있습니다 here. 마지막 코멘트에 대응


:이 조각은 나를 위해 컴파일 : fooconn_mgr 템플릿 내부의 멤버 함수입니다

void foo(std::string id) 
{ 
    conn_table_by_id_type& id_type_view = conn_table_.template get<0>(); 
    typename conn_table_by_id_type::const_iterator it = id_type_view.find(id); 
} 

. 위의 내용이 당신이하려는 일이라고 추측합니다.

세 가지 다른 conn_table_ 색인에 대한 참조를 얻는 도우미 메서드를 작성해야합니다. 이것은 훨씬 더 간결하게 만들 것입니다. 예 :

conn_table_by_id_type & by_id_type() {return conn_table_.template get<0>();} 

void foo2(std::string id) 
{ 
    typename conn_table_by_id_type::const_iterator it = by_id_type().find(id); 
} 
+0

Emile, thx a lot. 또한 typename을 사용하는 교훈을 배웠습니다. 예 : 내가 std :: vector :: iterator를 시도했을 때 오류가 발생했지만 일단 typename std :: vector :: iterator로 변환하면 효과적이다. – rjoshi

+0

안녕하세요 Emile, 위의 그림과 같이 conm_mgr 클래스를 사용할 때 "smpp_conn 클래스에"T "라는 형식이 없습니다."smpp_conn "클래스 객체를 conn_mgr 클래스의 첫 번째 템플릿 매개 변수"T "로 전달 중입니다 – rjoshi

+0

@ rjoshi : 수정 된 답변보기 –

관련 문제