shadow template

I would like to use T for all my templates...it's supposed to be the same kind so I dont know why it doesnt allow me to use T in my two templates..

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
#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 int get_r(){		//get real part
		int a = r;
		return a;
	}
	
	const int get_i(){		//get imaginarium part
		int a = i;
		return a;
	}
	
	
	template<typename T>
	friend ostream& operator<<(ostream & os,complex<T>& a);
         ////here is the error...if i do the same with N instead of T, It works.
         ///but i dont know the reason
};
	template<typename T>
	ostream& operator<<(ostream& os, complex<T>& a){
		os<<"Parte real "<<a.get_r()<<" "<<"Parte imaginaria "<<a.get_i()<<endl;
		return os;
	}



int main(){
	
	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<<c<<endl;
	
	complex<char> rojo;
	complex<char> azul(99,87);
	cout<<rojo<<azul<<c;
	
	
	
}
this code launch an error that is 







Because `T' already has a meaning.


http://stackoverflow.com/a/4661372

By the way, ¿why do you need a friend if you are using only the public interface?
Last edited on
yes, that's true I was gettting get_r and get_i which are public members...I guess I could use the operator . if I declare that function friend..

I dont understande properly what you mean about a meaning...

It's because both typenames are declared in the same scope and then there's a conflict??
because I can do this..

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
#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 int get_r(){		//get real part
		int a = r;
		return a;
	}
	
	const int get_i(){		//get imaginarium part
		int a = i;
		return a;
	}
	template<typename N>
	friend ostream& operator<<(ostream& os, complex<N>& a);
	
};
	template<typename T>
	ostream& operator<<(ostream& os, complex<T>& a){
		os<<"Parte real "<<a.r<<" "<<"Parte imaginaria "<<a.i<<endl;
		return os;
	}



int main(){
	
	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<<c<<endl;
	
	complex<char> rojo;
	complex<char> azul(99,87);
	cout<<rojo<<azul<<c;
	
	
	
}



Your example is equivalent to `the extrovert' on the previous link.
In this case the `operator<<' have access to all complex instantiations. By instance, you could operate with `complex<double>' when you are printing a `complex<char>'
It's quite promiscuous.

(note the example in `Taking advantage of the extrovert')


> I dont understande properly what you mean about a meaning...
> It's because both typenames are declared in the same scope and then there's a conflict??
Yes, it's like saying
1
2
int foo;
int foo;
thanks!!
Topic archived. No new replies allowed.