-3
class Complex
{
public:
    Complex(float = 0.0, float = 0.0); //default constructor that uses default arg. in case no init. are in main
    void getComplex(); //get real and imaginary numbers from keyboard
    void sum(Complex, Complex); //method to add two complex numbers together
    void diff(Complex, Complex); //method to find the difference of two complex numbers
    void prod(Complex, Complex); //method to find the product of two complex numbers
    void square(Complex, Complex); //method to change each complex number to its square
    void printComplex(); //print sum, diff, prod, square and "a+bi" form 

private: 
    float real; //float data member for real number (to be entered in by user)
    float imaginary; //float data member for imaginary number (to be entered in by user)
};

void Complex::getComplex()
{
    cout << "Enter real number: ";
    cin >> real;
    cout << "Enter imaginary number: ";
    cin >> imaginary;
}

void Complex::sum(Complex, Complex)
{
    float sum = 0.0;
    sum = real + imaginary;
}

int main()
{
    Complex c;
    c.getComplex();
    c.sum(Complex, Complex);
    c.diff(Complex, Complex);
    c.prod(Complex, Complex);
    c.square(Complex, Complex);
    c.printComplex();

    return 0;
}

I get an error under c.sum(Complex, Complex); inside the main (along with the c.diff, c.prod, and c.square lines). The error is:

type name Complex is not allowed and too few arguments in function call

I am not allowed to use overloading operators at all to complete this task. What should I do to resolve this? Code has been abbreviated to show relevant parts. Thanks again.

  • `c.sum(Complex, Complex);` => `c.sum(c, c);` Not it makes sense, but should fix the error message. – πάντα ῥεῖ Feb 11 '17 at 05:07
  • I'm sorry I offended you @Nawaz. – Mang0Marquez Feb 11 '17 at 05:11
  • 1
    @Mang0Marquez Your code is totally broken. I'd suggest you rewrite it completely from scratch. – πάντα ῥεῖ Feb 11 '17 at 05:12
  • @πάνταῥεῖ I wouldn't even know where to begin. Using `(c, c)` solved the squiggly red line, but then I got hidden errors at run-time. It said the variables inside `void Complex::printComplex()` needed `"&"` ...but when I put ampersands in there, it said it was an "illegal operation on bound member function expression"...I have a feeling this may be endless whack-a-mole with C++. – Mang0Marquez Feb 11 '17 at 05:15
  • And when I fix it to put it in front of `Complex` as in `void &Complex::printComplex()` I get a "reference to void is not allowed" – Mang0Marquez Feb 11 '17 at 05:17
  • 1
    @Mang0Marquez `void &Complex::printComplex()` What makes you think this should be valid c++ code? – πάντα ῥεῖ Feb 11 '17 at 05:18
  • I read this [this](http://stackoverflow.com/questions/28050325/illegal-operation-on-bound-member-function-expression) other S.O. answer. That's why. – Mang0Marquez Feb 11 '17 at 05:20
  • 1
    @Mang0Marquez That's a completely unrelated thing. – πάντα ῥεῖ Feb 11 '17 at 05:21
  • So how would I solve the issue of the `&: illegal operation on bound member function expression`? That was the error I got, so I thought it was pertinent... – Mang0Marquez Feb 11 '17 at 05:24
  • Also, I find it a little patronizing that @Nawaz said I haven't been reading books and articles to solve this problem. I've been steeped in this program since 11AM today. Should this program take weeks to finish by his standards? Or maybe a few minutes? What's the metric? Unbelievable snobbery. I've read so many different S.O. solutions that are unrelated that they're sending me around in circles. There's a limit to how much reading articles and books can accomplish. – Mang0Marquez Feb 11 '17 at 05:26
  • 3
    Some people may be a bit rude, but on the other hand some your code doesn't make any sense. Your `Complex::sum(Complex, Complex)` is a method on an object that takes two other instances and then doesn't even use them. As an analogy that's like inviting two other people over to your house to have a conversation, and then completely ignoring them while you talk to yourself. It's hard to help someone who may be misunderstanding the very fundamentals, and that looks like it may be the case here, so that's why people are assuming you haven't read a good book and are suggesting you do so. – TheUndeadFish Feb 11 '17 at 05:36
  • Oh hey, this looks rather similar to http://stackoverflow.com/questions/42171663/complex-number-program-compile-time-error-at-calculation-methods did you both get assigned homework today or something? – TheUndeadFish Feb 11 '17 at 05:51
  • *"I've been steeped in this program since 11AM today. Should this program take weeks to finish by his standards?"* - Yes, C++ is difficult, and if you've not really had programming experience before, it may well take weeks before you get all those basics right. Which book are you using? – Christian Hackl Feb 11 '17 at 11:24

1 Answers1

0

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.

CWurm
  • 1
  • 3