2010-12-27 5 views
0

Possible Duplicate:
Why doesn't a derived template class have access to a base template class' identifiers?C++ 템플릿, 컴파일러 오류

번역

#ifndef A_H 
#define A_H 
template <class T> 
class A 
{ 
    protected : 
    T a; 
    public: 
    A(): a(0) {} 
}; 
#endif 

는 BH는

#ifndef B_H 
#define B_H 
template <class T> 
class A; 

template <class T> 
class B: public A <T> 
{ 
    protected: 
    T b; 

    public: 
    B() : A<T>(), b(0) {} 
    void test() { b = 2 * a;} //a was not declared in this scope 
}; 
#endif 

오류가 발생합니다 : "는이 선언되지 않았습니다 범위". (Netbeans 6.9.1).

그러나 건설

void test() { b = 2 * this->a;} 

가 올 ... 문제는 어디에 있습니까?

전달 선언 또는 파일 포함 지시문을 사용하는 것이 더 좋습니까?

B.h

template <class T> 
class A; 

#include "A.h" 
+1

읽기 http://www.comeaucomputing.com/techtalk/templates/#whythisarrow –

+0

왜 [파생 된 템플릿 클래스가 기본 템플릿 클래스 '식별자에 액세스 할 수 없습니까?] (http : // stackoverflow. co.kr/questions/1239908/why-doesnt-a-derived-template-class-have-a-base-template-class-identi) –

답변

0

A<T>::a는 종속 이름입니다, 그래서 당신은이 규정 사용할 수 없습니다.

A<int>의 전문화 어딘가에 있다는 것을 상상해

template<> class A<int> { /* no a defined */ }; 

컴파일러는 이제 어떻게해야합니까? 또는 A<int>::a이 변수 대신 함수 인 경우 어떻게됩니까?

이미 this->a을 발견 했으므로 a에 액세스 할 수 있도록 설정하면 모든 것이 올바르게 작동합니다.

+0

오, 왜 설명없이 downvoted하는 것을 좋아합니까 :( –

+0

올바른 것. –