complex calculator

Hello there I am having some trouble with the coding of a complex calculator. I overload the operators (+-*/) to enable the calculation of complex numbers. At this point I don't understand why the compiler replies an error to me.

This is for adding a complex number to a double value like:

3 + 3+i2 or 3.7 + 3+i3

the private attributes are set to double. I declared this method outside the class because the left operant (a) isn't part of class 'complex'

1
2
3
4
5
6
7
8
  complex operator+(double a, complex b){
	complex temp;

	temp.re = a + b.getReal(); // Error
	temp.im = b.getImag(); // Error

	return temp;
}


Now the error occurs that the compiler tells me:

'im' is a private member of 'complex'
're' is a private member of 'complex'

but I use the Getter-Methods?

1
2
3
4
5
6
double complex::getReal(){
	return re;
}
double complex::getImag(){
	return im;
}


I can't understand this.
Last edited on
You use re/im of temp.

make the operator+ a friend of complex
Why do I use the re/im of temp?

I init a object of complex named temp and add the complex number and a double which is saved in temp.re

Now I declared this outside the class

1
2
3
4
	complex operator+(double a, complex b);
    complex operator-(double a, complex b);
    complex operator*(double a, complex b);
    complex operator/(double a, complex b);


and those inside the class. Compiler is ok with that just have to test if it is working properly.

1
2
3
4
    friend complex operator+(double, complex);
    friend complex operator-(double, complex);
    friend complex operator*(double, complex);
    friend complex operator/(double, complex);
Why do I use the re/im of temp?
Well:
1
2
	temp.re = a + b.getReal(); // <--- Access the private re -> temp
	temp.im = b.getImag();  // <--- Access the private im -> temp 



Do not declace the functions twice. friend might is ignored after the second declaration.
Topic archived. No new replies allowed.