My friend ostream function doesnt have acces

I have overloaded the operator << to display a defined type...for that I have declared that function as a friend and I have define two get functions..but for some reason It's not working..any help?

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

using namespace std;

//template<typename T>
class complex{
	int r;
	int i;
public:
	complex():r(0),i(0){};
	complex(int a):r(a),i(0){};
	complex(int a,int b):r(a),i(b){};
	
	
	complex (const complex& a){		//copy constructor
		r = a.r;
		i = a.i;
	}
	
	complex& operator=(const complex & 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& a);

};
	//template<typename T>
	ostream& operator<<(ostream& os,const complex& a){
		os<<"Parte real"<<get_r(a)<<" "<<"Parte imaginaria"<<get_i(a)<<endl;
		return os;
	}



int main(){
	
	cout<<"TESTEO DE CONSTRUCTOR Y COPY,ASSIGNAMENT, MOVE..ETC"<<endl;
	complex a;
	complex b(1);
	complex c(4,5);
	a = c;
	cout<<c<<endl;
	
	
}


There was a const where It didnt have to be...
¿where did you remove the const?
you should be able to print a temporary.


your class is not const-correct.
If a method does not modify the satus of the object, then it should be declared const
const int get_r() const;


> get_r(a)
¿is this legal now?
It was here..
1
2
3
4
5
6
7
8
9
10
//template<typename T>
	friend ostream& operator<<(ostream & os,complex& a);

};
	//template<typename T>
	ostream& operator<<(ostream& os,const complex& a){
		os<<"Parte real"<<get_r(a)<<" "<<"Parte imaginaria"<<get_i(a)<<endl;
		return os;
	}


the two functions were differents...so it didnt work...I'm trying to check what you said, and I think it's not working...

Neither declaring the functions as a friend not doing it...it says that get_r() is not in the scope...doing it like this get_r(a)...doing it like this a.get_r it doesnt give any problem...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class foo{
private:
	int value;
public:
	int get() const{ //so we can use it with constant objects
		return value;
	}
};

std::ostream& operator<<( //+no need to be friend
	std::ostream &out,
	const foo &a //so we can use it with temporaries and constant objects
)
{
	out << "Value: " << a.get() << '\n'; //+because we use the public interface
	return out;
}



About get_r(a), I think that there is a proposal to make it equivalent to a.get_r(), in the current standard only the later is valid.
> I have overloaded the operator << to display a defined type...for that I have declared that function as a friend

Strongly favour overloading the operator as a non-friend function.

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

template< typename T = double > class complex {

    static_assert( std::is_arithmetic<T>::value, "T must be an arithmetic type" ) ;
    T r;
    T i;

    public:
        complex() : r(0),i(0) {};
        complex( T a ) : r(a), i(0) {};
        complex( T a, T b ) : r(a), i(b) {};

        T get_r() const { return r ; }	//get real part
        T get_i () const { return i ; } //get imaginarium part
};

template<typename T> // non-friend, non-member. note: a.get_r(), a.get_i()
std::ostream& operator<< ( std::ostream& os, complex<T>& a) {
    return os << "( Parte real " << a.get_r() << ", Parte imaginaria " << a.get_i() << " )" ;
}

int main() {

	std::cout<<"TESTEO DE CONSTRUCTOR Y COPY,ASSIGNAMENT, MOVE..ETC\n" ;
	complex<> a;
	complex<> b(1);
	complex<> c(4,5);
	a = c;
	std::cout << c << '\n' ;
}

http://coliru.stacked-crooked.com/a/6b6389919a33cba9

The syntax for declaring a non-inline template friend:

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

template< typename T > class complex ; // declare complex

template< typename T > std::ostream& operator<< ( std::ostream& os, complex<T>& a ) ; // declare friend

template< typename T = double > class complex {

    static_assert( std::is_arithmetic<T>::value, "T must be an arithmetic type" ) ;
    T r;
    T i;

    public:
        complex() : r(0),i(0) {};
        complex( T a ) : r(a), i(0) {};
        complex( T a, T b ) : r(a), i(b) {};

        T get_r() const { return r ; }	//get real part
        T get_i () const {	return i ; }	//get imaginarium part

        // use <> after the function name to indicate that the friend is a template
	friend std::ostream& operator<< <> ( std::ostream& os, complex<T>& a );

};

template<typename T> // define friend function
std::ostream& operator<< ( std::ostream& os, complex<T>& a ) {
    return os << "( Parte real " << a.r << ", Parte imaginaria " << a.i << " )" ;
}

int main() {

	std::cout<<"TESTEO DE CONSTRUCTOR Y COPY,ASSIGNAMENT, MOVE..ETC\n" ;
	complex<> a;
	complex<> b(1);
	complex<> c(4,5);
	a = c;
	std::cout << c << '\n' ;
}

http://coliru.stacked-crooked.com/a/5872e37435ab86bc
Topic archived. No new replies allowed.