template derivaded

I'm trying to get a constructor well done from a derivaded template but it's giving an error...the default constructor is working proprely but the next ones dont compile...what it could be?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

template<typename T>
class complex{
	T r;
	T i;
public:
	complex<T>():r(0),i(0){};
	complex<T>(T a):r(a),i(0){};
	complex(T a,T b):r(a),i(b){};
 
template<typename T>
class Scomplex: public complex<T>{
	T z;
public:

	Scomplex<T>():complex<T>(),z(0){};
	Scomplex<T>(T a,T b):complex<T>(T a),z(b){};
	Scomplex<T>(T a,T b,T c):complex<T>(T a,T b),z(c){};
};
In fact what I would like to do is two have two differents types... T for r and i and...other for z...

I know it would be easier just doing one template like this...
1
2
3
4
5
6
7
template<typename T,typename N>
class Scomplex{
        T  r;
        T  i;
        N  z;

}


but I would like to be able to do some operation with just complex...and as well I would like to know how to do it..


Scomplex<T>(T a,T b):complex<T>(T a),z(b){};
complex<T>(T a) ← this is not how you call a funtion.
i dont understand what you mean...my constructor is how the first line that you wrote...

I guess the trouble is when i call to do constructor from the base class...but what happens??
yes, I got it...I was kind of declaring a constructor base again..when I had just to call it...

and do you know how I could combine what I did but with two...typename...??

Thanks!!!
Why not
1
2
3
4
5
6
7
8
9
template<typename T, typename N>
class Scomplex: public complex<T>{
	N z {}; //Value initialize variable if not initialized itherwise
public:

	using complex<T>::complex<T>; //Inherit three complex constructors
	//z will be value initialized in all those cases
	Scomplex(T a,T b, N c): complex<T>(T a,T b), z(c) {};
};
yes, it makes sense...but I cant see the meaning of the brackets here N z {}

and this declaration using complex<T>::complex <T>..i dont know if it saves you to define the constructor again because doing using they are already declared....but being comples<T> public the constructor are already declared...

Thanks for the help
It might be c++11 or 14 that let you initialize an attribute in the class declaration . The keyword using is like a shortcut think of it like , in the declared scope ( current class scope) , you won't need to write it , unless it becomes ambiguous . You can also use the keyword using with a method scope if you want to avoid writing multiple class/namespace scopes for instance.
, in the declared scope ( current class scope) , you won't need to write it
You need to write tit to inherit constructors as soon as you declare first one. Else it will hide them.
if I type using ig gives me some errors with c++11, but the initiaizer it admits it...but i dont understand why it could be done and why, since all the constructor are defined.....

would it make sense in case I dont declare constructors for Scompex??
would it make sense in case I dont declare constructors for Scompex??
It depends on what you need. Do you need to be able to initialize z varible in constructor? Probably yes.

Technically you do not have to inherit constructors: you can always write your own delegating constructors.

I was asking why cannot inherit from complex<T> and add another template type for variable z if you need it.
yes, but what I dont understand is I can't see the relation declaring brackets next to the data member declaration...because declaring and defining a constructor I'am already setting and initial value on the data member...I can't see how It would work...
is I can't see the relation declaring brackets next to the data member declaration
You do not strictly need that either.

It is C++11/14 feature called in-class braces-or-equals memner initializer.
It is saying "it you won't initialize this member in constructor, initialize it with this value".

I used that to make use of inheriting constructors, because Complex constructors knows nothing about z and will not initialize it. Empty brackets are used to perform value-initialization, which for arithmetic types means zero-initialization. So for arithmetic types:
1
2
3
4
N z {};
//Is same as
N z = 0;
N z {0};
ok, I get it, so then supossing that I just call to the complex constructor, I wouldnt have to define Scomplex constructor if I dont mean to initialize with cero??....

thanks!!!
I wouldnt have to define Scomplex constructor if I dont mean to initialize with cero??....
Firs of all determine what you want. Do you want to allow constructing Scomplex with fewer than 3 arguments? If so, what should happen? What value should your members have after construction? After you will decide what should be done and make a list of constructors you want and their behavior, you can start implementing them.
yes, I see...what I want is that when I dont type a parameter in a declaration object it must be by default cero..
I have gone forward with my examples, and I have created this MN complex which is a class that inherit everything from the rest, there's one base class, two derivated from the base, and one more derivated....what I'm trying to do is just play a bit with virtual base class...

the last class has a constructor which is inherited from the rest...it can build objects...but I'm trying to display the data membre through an ostream...I though that I have inherited the methods for that, but it's not working....can you have a look??, thanks!!!
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>

using namespace std;

template<typename T>
class complex{
	T r;
	T i;
public:
	complex<T>():r(0),i(0){};
	complex<T>(T a):r(a),i(0){};
	complex(T a,T b):r(a),i(b){};
	
	
	complex<T> (const complex<T>& a){		//copy constructor
		r = a.r;
		i = a.i;
	}
	
	complex<T>& operator=(const complex<T> & a){ // copy assignament;
		
		r = a.r;
		i = a.i;
		return *this;
	}
	
	const T get_r() const{		//get real part
		T a = r;
		return a;
	}
	
	const T get_i() const{		//get imaginarium part
		T a = i;
		return a;
	}
	
};
	template<typename T>
	ostream& operator<<(ostream& os,const  complex<T>& a){
		os<<"Parte real "<<a.get_r()<<" "<<"Parte imaginaria "<<a.get_i()<<endl;
		return os;
	}
template<typename T,typename N>
class Ncomplex:virtual public complex<T>{
	N z; 
	
public:

	Ncomplex<T,N>():complex<T>(),z(0){};
	Ncomplex<T,N>(T a,N b):complex<T>(a),z(b){};
	Ncomplex<T,N>(T a,T b,N c):complex<T>(a,b),z(c){};
	
	const N get_z() const {
		N a = z;
		return a;
	}
	
	
};

template<typename T,typename N>
ostream& operator<<(ostream& os,const Ncomplex<T,N>& a){
	os<<" z: "<<a.get_z()<<" r: "<<a.get_r()<<" i: "<<a.get_i()<<endl;
	return os;
}



template<typename T,typename M>
class Mcomplex: virtual public complex<T>{
	
	M y;
public:
	Mcomplex<T,M>():complex<T>(),y(0){};
	Mcomplex<T,M>(T a,M b):complex<T>(a),y(b){};
	Mcomplex<T,M>(T a,T b,M c):complex<T>(a,b),y(c){};
	
	
	const M get_y()const{
		M a;
		a = y;
		return a;
	}
};
template<typename T,typename M>
ostream& operator<<(ostream& os,const Mcomplex<T,M>& a){
	os<<" y: "<<a.get_y()<<" r: "<<a.get_r()<<" i: "<<a.get_i()<<endl;
	return os;
}


template<typename T,typename M,typename N>
class MNcomplex: public Mcomplex<T,M>,public Ncomplex<T,N>{
	public:
	MNcomplex<T,M,N>(T a,T b,M c,N d):Mcomplex<T,N>(a,b,d),Ncomplex<T,M>(a,b,c){};
	
};

template<typename T,typename M,typename N>
ostream& operator<<(ostream& os,const MNcomplex<T,M,N>& a){
	os<<" z: "<<a.get_z()<<" y: "<<a.get_y()<<" r "<<a.get_r<<" i "<<a.get_i()<<endl;
	return os;
}



int main(){
	

	Ncomplex<int,int> aver;
	Ncomplex<int,int> yeste(3,5);
	Ncomplex<int,int> last(5,7,8);
	Ncomplex<int,char> last1(5,7,'a');
	Mcomplex<int,char> new1(7,5,'r');
	MNcomplex<int,char,char> todo(5,6,'a','t');
	cout<<todo<<endl;
	
	cout<<"TESTEO DE CONSTRUCTOR Y COPY,ASSIGNAMENT, MOVE..ETC"<<endl;
	complex<int> a;
	complex<int> b(1);
	complex<int> c(4,5);
	complex<int> t(a);
	a = c;
	cout<<last1<<new1;

}




Spot the problem: a.get_r<<" i "<<a.get_i()<<
thanks, the compiler doesnt give you a clue at all...
Topic archived. No new replies allowed.