0
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)
};

Complex::Complex(float r, float i)
{   
  real = r;
  imaginary = i;
}

void Complex::sum(Complex r, Complex i)
{
  sum = r + i; //error here under "sum" and "+"
}

int main()
{
  Complex c;
  c.getComplex();
  c.sum(Complex r, Complex i); //error here under "Complex r"
  c.diff(Complex r, Complex i); //error here under "Complex r"
  c.prod(Complex r, Complex i); //error here under "Complex r"
  c.square(Complex r, Complex i); //error here under "Complex r"
  c.printComplex();

  return 0;
}

I am not allowed to user overloading at all.

I have several errors, but they have a common thread, I believe. They occur at the calculation points in my methods. I don't know how to fix it. I believe I have the public and private parts down, though.

Here are my errors:

Error 7 error C2676: binary '+' : 'Complex' does not define this operator or a conversion to a type acceptable to the predefined operator c:\users\Sarah\desktop\classwork\c++\exam 3 program\exam 3 program\exam 3 program.cpp 37

Error 16 error C3867: 'Complex::sum': function call missing argument list; use '&Complex::sum' to create a pointer to member c:\users\Sarah\desktop\classwork\c++\exam 3 program\exam 3 program\exam 3 program.cpp 58

Error 2 error C2784: 'std::_String_const_iterator<_Elem,_Traits,_Alloc> std::operator +(_String_const_iterator<_Elem,_Traits,_Alloc>::difference_type,std::_String_const_iterator<_Elem,_Traits,_Alloc>)' : could not deduce template argument for 'std::_String_const_iterator<_Elem,_Traits,_Alloc>' from 'Complex' c:\users\Sarah\desktop\classwork\c++\exam 3 program\exam 3 program\exam 3 program.cpp 37

Community
  • 1
  • 1
  • And? What are the errors? – Greg Kikola Feb 11 '17 at 02:55
  • See the code. It's in the comments. I'll type them out too in the main part of my question. – SarahSanchez Feb 11 '17 at 02:56
  • Yes, but what are the actual errors? – Greg Kikola Feb 11 '17 at 03:00
  • Ah! Silly me. `"expression must be a modifiable Ivalue"` and "no operator matches these operands" and in the main, `"type name is not allowed & too few arg in funct call" ` – SarahSanchez Feb 11 '17 at 03:01
  • relevant documentation on SO re: these errors wasn't very pertinent to my question, or if it was, i couldn't decipher it to apply to this code without having more knowledge. – SarahSanchez Feb 11 '17 at 03:03
  • @SarahSanchez Post a [MCVE] including the verbatim error text please. – πάντα ῥεῖ Feb 11 '17 at 03:07
  • You're performing operations on `Complex` like `+` and `*` but you haven't defined those operators. That's one of your problems. – Greg Kikola Feb 11 '17 at 03:09
  • @Greg, how do I define those operators without overloading them? – SarahSanchez Feb 11 '17 at 03:12
  • Well if you don't want to use operator overloading you'll have to define those functions in terms of the built-in types and their operators. Instead of just doing `r + i` you'll have to add the corresponding real parts and imaginary parts separately. – Greg Kikola Feb 11 '17 at 03:17
  • I changed `r` and `i` to `real` and `imaginary` respectively, and it got rid of the operator error! Thank you. BUT! I still have an error underneath `sum`. What am I missing there? – SarahSanchez Feb 11 '17 at 03:23
  • Also, if you could clarify, why does using `real` and `imaginary` work (because it does)? I'm very curious. thanks again. – SarahSanchez Feb 11 '17 at 03:39
  • Oh hey, this looks rather similar to http://stackoverflow.com/questions/42172395/type-name-is-not-allowed-cannot-use-overloading did you both get assigned homework today or something? – TheUndeadFish Feb 11 '17 at 05:52

1 Answers1

0
void Complex::sum(Complex r, Complex i)
{
    sum = r + i; //error here under "sum" and "+"
}

you are trying to use operator '+' for two Complex instances. It is not defined. variable sum is not defined either. I can guess you wanted to implement c = x + y or c.sum(x,y). Then this function should look like this.real = x.real + y.real; this.img = x.img + y.img

Also you cannot define variables inside function call like this c.sum(Complex r, Complex i);