Help with templates and inheritance

I have two classes that I want to use templates with but I get this error:

error C2989: 'B' : class template has already been declared as a non-class template

I searched in google and I tried to change some stuff in my code but still didn't work.

Can someone help me please?


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
  #include <iostream>

using namespace std;

template <typename T>
class A
{
	T valuea;

public:
	A(){};
	T getValuea() const { return valuea; }
	void setValuea(T x) { valuea = x; }

	A(const A &x){ valuea = x.valuea; }         //copy constructor

	friend class B;
};

template <typename T>
class B : A<T>
{
	T valueb;

public:
	B(){};
	T getValueb() const { return valueb; }
	void setValueb(T x) { valueb = x; }
	B(const B &x)
	{
		valueb = x.valueb;
		valuea = x.valuea;
	}

};

int main(){

	B<int> b1;
	b1.setValuea(5);
	b1.setValueb(10);
	B b2(b1);

	cout << "b2.valuea = " << b2.getValuea() << " b2.valueb = " << b2.getValueb() << endl;

	system("Pause");
	return 0;

}
In line 17 friend class B; you are saying that there is a class 'B' that is not a template.
then in line 21 you define a class 'B' that it is a template
1
2
3
4
5
6
7
template <typename T> class B; //forward declare

template <typename T>
class A{
   //...
   friend class B<T>; //A<int> is a friend of B<int>, but not of B<double>
};


You've got other errors
1
2
3
4
5
6
7
8
9
10
11
	B(const B &x)
	{
		valueb = x.valueb;
		this->valuea = x.valuea; //need 'this' (i don't know the rationale, but it is related with your private inheritance)
	}


B<int> b2(b1); //you need to provide the template argument

b1.setValuea(5); //inaccessible
b2.getValuea() //inaccessible 


To make setValue() and getValue() work
1
2
3
4
5
6
7
template <typename T>
class B : A<T>
{
public:
	using A<T>::setValuea;
	using A<T>::getValuea;
//... 
Thank you so much!

I fixed the errors in the code.

Now I am having some problems with strings, struct Date.

Can you have a look, please?

I am trying to setvalues, and get them in int main to test that my classes work fine.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
using namespace std;

template <typename T> class B;     //forward declare

template <typename T>
class A
{
	T valuea;
public:
	A(){};
	T getValuea()
	{
		return valuea;
	}
	
	void setValuea(T x)
	{
		valuea = x;
	}
	A(const A &x)
	{ 
		valuea = x.valuea; 
	}

	friend class B<T>;     //A<int> is a friend of B<int> 
};

template <typename T>
class B : A<T>
{
	T valueb;
public:
	using A<T>::setValuea;
	using A<T>::getValuea;
	B(){};
	T getValueb()
	{
		return valueb;
	}
	void setValueb(T x)
	{
		valueb = x;
	}
	B(const B &x)
	{
		valueb = x.valueb;
		this->valuea = x.valuea;
	}
};

struct Date
{
	int day;
	int month;
	int year;
};

int main()
{
	B<float> b;
	b.setValuea(1.34);
	b.setValueb(3.14);

	cout << "b.setValuea(1.34): " << b.getValuea() << endl
		<< "b.setValueb(3.14): " << b.getValueb() << endl;

	B<int> a;
	a.setValuea(1);
	a.setValueb(3);

	cout << "a.setValuea(1): " << a.getValuea() << endl
		<< "a.setValueb(3): " << a.getValueb() << endl;

	B<char> y;
	y.setValuea('a');
	y.setValueb('c');

	cout << "y.setValuea('a'): " << y.getValuea() << endl
		<< "y.setValueb('c'): " << y.getValueb() << endl;

	B<string> u;
	u.setValuea("good");
	u.setValueb("morning");

/*	cout << "u.setValuea(good): " << u.getValuea << endl;
	cout << "u.setValueb(morning): " << u.getValueb << endl; */

	/*B<Date> p;
	p.setValuea = { 27, 10, 2014 };
	p.setValueb = { 2, 11, 2014 };*/



	system("Pause");
	return 0;
}
> Now I am having some problems with strings, struct Date.
¿what problems? (doesn't compile, crash, undesired output)
You've forgot the parenthesis u.getValue()
I fixed that, I am having a problem with how to pass a struct Datatype into a my class.

Look at struct Date. I want to pass it into the function setValuea, setValueb and return it by getValuea, getValueb.

but I am doing some syntax mistake. Can you help me?

Edit: It doesn't compile error.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <string>
using namespace std;

template <typename T> class B;     //forward declare

template <typename T>
class A
{
	T valuea;
public:
	A(){};
	T getValuea()
	{
		return valuea;
	}
	
	void setValuea(T x)
	{
		valuea = x;
	}
	A(const A &x)
	{ 
		valuea = x.valuea; 
	}

	friend class B<T>;     //A<int> is a friend of B<int> 
};

template <typename T>
class B : A<T>
{
	T valueb;
public:
	using A<T>::setValuea;
	using A<T>::getValuea;
	B(){};
	T getValueb()
	{
		return valueb;
	}
	void setValueb(T x)
	{
		valueb = x;
	}
	B(const B &x)
	{
		valueb = x.valueb;
		this->valuea = x.valuea;
	}
};

struct Date
{
	int day;
	int month;
	int year;
};

int main()
{
	B<float> b;
	b.setValuea(1.34);
	b.setValueb(3.14);

	cout << "b.setValuea(1.34): " << b.getValuea() << endl
		<< "b.setValueb(3.14): " << b.getValueb() << endl;

	B<int> a;
	a.setValuea(1);
	a.setValueb(3);

	cout << "a.setValuea(1): " << a.getValuea() << endl
		<< "a.setValueb(3): " << a.getValueb() << endl;

	B<char> y;
	y.setValuea('a');
	y.setValueb('c');

	cout << "y.setValuea('a'): " << y.getValuea() << endl
		<< "y.setValueb('c'): " << y.getValueb() << endl;

	B<string> u;
	u.setValuea("good");
	u.setValueb("morning");

	cout << "u.setValuea(good): " << u.getValuea() << endl
		<< "u.setValueb(morning): " << u.getValueb() << endl;

	B<Date> p;
	p.setValuea({ 27, 10, 2014 });
	p.setValueb({ 2, 11, 2014 });

	cout << "p.setValuea({ 27, 10, 2014 }): " << p.getValuea() << endl
		<< "p.setValueb({ 2, 11, 2014 }):  " << p.getValueb() << endl;


	system("Pause");
	return 0;
}
Last edited on
cout doesn't have an overloaded operator that takes in a Date object.
Either you got to make one,

1
2
3
4
5
ostream& operator<<(ostream& os, const Date& dt)
{
    os << dt.day << '/' << dt.month << '/' << dt.year;
    return os;
}


or get the individual values of Date.
Last edited on
Topic archived. No new replies allowed.