Assumption: the sum, diff etc. methods of your class are not supposed to return any result or modify the class instances that are passed to them, but only display the result of the operation via std::cout.
Please read at least http://www.cplusplus.com/doc/tutorial/functions/ in order to understand how values are passed to functions (or in this case methods, see http://www.cplusplus.com/doc/tutorial/classes/ )
While you can declare a method only by writing the types of the arguments, you have to specify identifiers in the definition:
`
void Complex::sum(Complex c1, Complex c2)
{
}
Then you can access the members of c1 and c2, like c1.imaginary
A more efficient way to pass c1 and c2 would be to use "const references" const Complex& c1
. Then no copy of the objects will be made.
Actually, the methods sum, diff, prod could be made static methods, as they do not have effect on the Complex object c they are called on in the main(following my assumption). So it should rather be
Complex::sum(c1, c2);
If my assumption is wrong, and c.sum()
shall actually have an effect on c, I do not understand why you need two arguments for the method. You could just add c1 to c. However, the method should then be called Add
. Or you have to return the sum as a new object, but the method was declared void.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…