templates and inheritance

Hi,
I can't understand why I get an error when I try co compile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

template<typename T>
class A
{
public:
  A() {std::cout << "constructor A\n";}
  ~A() {std::cout << "destructor A\n";}
  T Apublic;
protected:
  T Aprotected;
private:
  T Aprivate;
};

template<typename T>
class B : public A<T>
{
public:
  B() {std::cout << "constructor B\n";}
  ~B() {std::cout << "destructor B\n";}
  void init() {Apublic = 0; Aprotected = 1;}
  T pub() {return Apublic;}
  T pro() {return Aprotected;}
};

int main()
{
B<double> a;
a.init();
std::cout << a.pub() << a.pro() << "end!\n";
return 0;
}

The error massage says
: In member function 'void B<T>::init()':
:22: error: 'Apublic' was not declared in this scope
:22: error: 'Aprotected' was not declared in this scope
: In member function 'T B<T>::pub()':
:23: error: 'Apublic' was not declared in this scope
: In member function 'T B<T>::pro()':
:24: error: 'Aprotected' was not declared in this scope
This is due to the fact that Apublic, Aprotected, etc may not exist in a template specialization of A.

See this for more:

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.19
Topic archived. No new replies allowed.