2010-12-27 10 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。 com/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

あなたの答えは正しい。カウンターバランスにアップアップ。 –

関連する問題