0

I want to overload operator/ in a class. to do it I want to define private inversion method which calculate inversion of first complex number(1/Z1) and then multiply by the second complex number. I developed the code which give me an error

Exc20.cc: In function ‘ComplexNumber operator/(const ComplexNumber&, const ComplexNumber&)’:
Exc20.cc:75:31: error: ‘inversion’ was not declared in this scope
return ComplexNumber(com1 * inversion(com2));

and the body of the code is

#include <iostream>
#include <cmath>

using namespace std;

class ComplexNumber
{
public:
  double re;
  double im;
  ComplexNumber(double real = 0, double imag = 0): re{real}, im{imag} {};

private:
  ComplexNumber& inversion(const ComplexNumber& com);
};

ComplexNumber& ComplexNumber::inversion(const ComplexNumber& com)
{
  re =  com.re/sqrt(com.re*com.re + com.im*com.im);
  im = -com.im/sqrt(com.re*com.re + com.im*com.im);
  return *this;
}

ComplexNumber operator*(const ComplexNumber& com1,const ComplexNumber& com2)
{
  return ComplexNumber(com1.re*com2.re-com1.im*com2.im,com1.re*com2.im+com1.im*com2.re);
}

ComplexNumber operator/(const ComplexNumber& com1, const ComplexNumber& com2)
{
  return ComplexNumber(com1 * inversion(com2));
}

int main()
{
  ComplexNumber com1(3,7);
  ComplexNumber com2(4,2);
  com2.print();
  com3=com1/com2;
  com3.print();
}
  • `inversion` is a `ComplexNumber` member function. It needs to be invoked on a `ComplexNumber` object. – user4581301 Dec 02 '18 at 06:34
  • Consider using operators as member functions. – 273K Dec 02 '18 at 06:35
  • Something else to note: You made both real and imag default to 0. Which means you may want to mark that constructor as `explicit` or it may surprisingly create ComplexNumber objects with just an integer like `7`. Or maybe that's what you want. – Zan Lynx Dec 02 '18 at 06:40
  • There is a strong case to be made for making `inversion` a free function that returns a new `ComplexNumber` rather than turning `this` into the inverse of `com`. – user4581301 Dec 02 '18 at 06:41

2 Answers2

0

Your inversion function is a class member which means it has to be called on a class object. The way you have it in your code is calling it as if it was a free function or class static.

You could make it class static. Or you could change it so you can call it on an object like com2.inversion()

Making it private means that operator/ cannot call it. However, if you were to put your operator functions inside your class and make them friends it would work. Like so:

friend ComplexNumber operator/(const ComplexNumber& com1, const ComplexNumber& com2)
Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
0

The compiler cannot find inversion in operator/ because inversion is a ComplexNumber member function and needs to be invoked on an instance of ComplexNumber. In addition it is a private member and cannot accessed outside ComplexNumber except by ComplexNumber's friends.

As a member function, inversion operates on a ComplexNumber instance, changing it into the inverse of the com parameter. Most uses of this would require the user to create a temporary ComplexNumber to store the inversion. Instead we can make inversion a free function that returns a new ComplexNumber.

ComplexNumber inversion(const ComplexNumber& com)
{
    ComplexNumber temp;
    temp.re = com.re / sqrt(com.re * com.re + com.im * com.im);
    temp.im = -com.im / sqrt(com.re * com.re + com.im * com.im);
    return temp;
}

This solves all of the problems of

ComplexNumber operator/(const ComplexNumber& com1, const ComplexNumber& com2)
{
    return ComplexNumber(com1 * inversion(com2));
}

Assembled example

#include <iostream>
#include <cmath>

using namespace std;

class ComplexNumber
{
public:
    double re;
    double im;
    ComplexNumber(double real = 0, double imag = 0) : re { real }, im { imag }
    {
    }
};

ComplexNumber inversion(const ComplexNumber& com)
{
    ComplexNumber temp;
    temp.re = com.re / sqrt(com.re * com.re + com.im * com.im);
    temp.im = -com.im / sqrt(com.re * com.re + com.im * com.im);
    return temp;
}

ComplexNumber operator*(const ComplexNumber& com1, const ComplexNumber& com2)
{
    return ComplexNumber(com1.re * com2.re - com1.im * com2.im,
                         com1.re * com2.im + com1.im * com2.re);
}

ComplexNumber operator/(const ComplexNumber& com1, const ComplexNumber& com2)
{
    return com1 * inversion(com2); // operator * already makes a new ComplexNumber. No 
                                   // need to make another
}

int main()
{
    ComplexNumber com1(3, 7);
    ComplexNumber com2(4, 2);
    ComplexNumber com3 = com1 / com2;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54