Template subclass of a template superclass

Hi! I'm trying to run this code, nevertheless complier keeps on telling me further errors...any idea? What kind of theme should i be looking for in a book, in order to solve my answer? Thanks a lot!!!


#include <iostream>
using namespace std;

template<class T>
class A{
protected:
T a;
public:
void setA(T);
T getA();
};

template<class T>
void A<T>::setA(T a){
this->a = a;
}

template<class T>
T A<T>::getA(){
return a;
}

template<class T>
class B : public A<T>{
public:
T bGetA();
void bSetA(T);
};

template<class T>
T bGetA(){
return a;
}

template<class T>
void bSetA(T a){
this->a = a;
}

int main(){
B b;
b.bSetA();
cout << b.GetA()<<endl;
return 0;
}

HerenciaT.cpp: In function ‘T bGetA()’:
HerenciaT.cpp:32: error: ‘a’ was not declared in this scope
HerenciaT.cpp: In function ‘void bSetA(T)’:
HerenciaT.cpp:37: error: invalid use of ‘this’ in non-member function
HerenciaT.cpp: In function ‘int main()’:
HerenciaT.cpp:41: error: missing template arguments before ‘b’
HerenciaT.cpp:41: error: expected ‘;’ before ‘b’
HerenciaT.cpp:42: error: ‘b’ was not declared in this scope
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
using namespace std;

template<class T>
class A
{
protected:
	T a;
public:
	void setA(T);
	T getA();
};

template<class T>
void A<T>::setA(T a)
{
	this->a = a;
}

template<class T>
T A<T>::getA()
{
	return a;
}

template<class T>
class B: public A<T>
{
public:
	T bGetA();
	void bSetA(T);
};

template<class T>
T B<T>::bGetA() // Forgot to add class name qualifier
{
	return this->a;
}

template<class T>
void B<T>::bSetA(T a) // Forgot to add class name qualifier
{
	this->a = a;
}

int main()
{
	B<int> b; // forgot template parameter
	b.bSetA(1); // forgot function parameter
	cout << b.bGetA() << endl; // Got function name wrong
	return 0;
}
Topic archived. No new replies allowed.