2015-02-07 2 views
0

임 프로그램 A, I는 구조체가 다른 구조체를 사용하여 노드라고 선언하고 템플릿 클래스에 정의 된 NODE_TYPE라고 ab_int 내가 사용하고 lib 디렉토리 B에 의해 제공.자신의 구조체에 템플릿 클래스의 구조체를 사용하는 방법은 무엇입니까?

프로그램 A : LIB B에서 구조체 에게 NODE_TYPE이 포함 된 템플릿

struct Node { 
int rank; 
ab_int::node_type nt; 

Node() : rank(), nt() {} 
Node(int rank_new, ab_int::node_type nt_new) : rank(rank_new), nt(nt_new) {} 
}; 

이된다, 또한 내가 원하는 기능 루트() LIB B에서

template<class t_bitvector = bit_vector, ...> 
class ab_int 
{ 
    ... 
    public: 
    struct node_type { 
    ... 
    // Assignment operator 
    node_type& operator=(const node_type&) = default; 

    // Move assignment operator 
    node_type& operator=(node_type&&) = default; 
    ... 
    } 

    ... 
    node_type root() const { 
     return node_type(0, m_size, 0, 0); 
    } 
    ... 
} 

있다 사용.

// wt instantiated (works!) 
Node node; 
node.rank = 1; 
node.nt = wt.root(); 

나는 다음과 같은 컴파일 오류가 점점 오전 :

.../main.cpp:23:5: error: expected a class or namespace 
ab_int::node_type nt; 
^ 
.../main.cpp:26:44: error: expected a class or namespace 
Node(int rank_new, ab_int::node_type nt_new) : rank(rank_new), nt(nt_new) {} 

업데이트 1 :

는 프로그램 A, 나는 노드 구조체의 새 인스턴스를 인스턴스화 할

ab_int::node_typeab_int<>::node_type으로 변경하면 다른 오류가 팝업 :

.../main.cpp:63:13: error: no viable overloaded '=' 
node.nt = wt.root(); 
~~~~~~~^~~~~~~~~~ 
.../ab_int.hpp:752:24: note: candidate function not viable: no known conversion from 'z::ab_int<z::rrr_vector<63, z::int_vector<'\x00'>, 32>, z::rank_support_rrr<'\x01', 63, z::int_vector<'\x00'>, 32>, z::select_support_rrr<'\x01', 63, z::int_vector<'\x00'>, 32>, z::select_support_rrr<'\x00', 63, z::int_vector<'\x00'>, 32> >::node_type' to 'const z::ab_int<z::int_vector<'\x01'>, z::rank_support_v<'\x01', '\x01'>, z::select_support_mcl<'\x01', '\x01'>, z::select_support_mcl<'\x00', '\x01'> >::node_type' for 1st argument 
     node_type& operator=(const node_type&) = default; 
       ^
.../ab_int.hpp:755:24: note: candidate function not viable: no known conversion from 'z::ab_int<z::rrr_vector<63, z::int_vector<'\x00'>, 32>, z::rank_support_rrr<'\x01', 63, z::int_vector<'\x00'>, 32>, z::select_support_rrr<'\x01', 63, z::int_vector<'\x00'>, 32>, z::select_support_rrr<'\x00', 63, z::int_vector<'\x00'>, 32> >::node_type' to 'z::ab_int<z::int_vector<'\x01'>, z::rank_support_v<'\x01', '\x01'>, z::select_support_mcl<'\x01', '\x01'>, z::select_support_mcl<'\x00', '\x01'> >::node_type' for 1st argument 
     node_type& operator=(node_type&&) = default; 

업데이트 2 :

을 두 번째 컴파일 오류가 개체 중량의 인스턴스에 대한 약간의 불일치와 관련이 있었다. 원래 질문과 관련이 없으므로 더 이상 논의하지 않습니다.

+0

다른 질문을하기 위해 질문을 업데이트하지 마십시오. – Barry

+0

두 번째 질문 인 'node.nt'와 'wt.root()'는 다른 유형이 있습니다 (첫 번째 템플릿 arg는 'z :: rrr_vector <>'와'z : int_vector <>'입니다). 다른 쪽). – Barry

+0

it 's z : rrr_vector <>, 실수를 수정했습니다 ... – Scholle

답변

2
ab_int이 형이 아니기 때문에의

는, 그것의 템플릿 :

template<class t_bitvector = bit_vector, ...> 
class ab_int { .. }; 

그리고 당신이 유형처럼 사용하고 있습니다 :

struct Node { 
    int rank; 
    ab_int::node_type nt; 
// ^^^^^^^^ 

는 따라서 "에 오류가 클래스를 예상하거나 네임 스페이스 ". 당신은 문법적으로 그런 템플릿을 사용할 수 없습니다. 당신이 ab_int (이 귀하의 사용 사례에 얼마나 잘 맞는지에 따라)로 그 유형을 전달 Node 자체가 템플릿을 만들 수있는, 또는

ab_int<bit_vector_type, ...>::node_type nt; 
ab_int<>::node_type nt; // use defaults 

: 올바른 구문은 ab_int 템플릿의 특정 인스턴스를 제공하는 것입니다 : 지금 node_type는 의존의 형태되기 때문에 우리는 추가 typename 키워드를 제공해야 할 것입니다 그 원인에

template <typename t_bitvector = bit_vector, ...> 
struct Node { 
    typename ab_int<bitvector, ...>::node_type nt; 
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
}; 

.

0

수동 회피 내가 위로 읽는 게 좋을 것 템플릿 인수를 지정하고자하는 경우 : 템플릿을보다 유연하고 유지하기 쉽게 만들 수 템플릿 인수 공제를 사용 http://en.cppreference.com/w/cpp/language/template_argument_deduction

합니다. 템플릿은 일반적인 코드를 작성하는 데 사용되므로 템플릿 유형 매개 변수를 수동으로 제공하면 해당 코드가 제한됩니다.

관련 문제