0

I have been trying to compile this program but it is giving me an error in regards to overloading the * operator for one of the functions: complex operator *(double n)const

When I try to compile I get the error: no match for 'operator*' in '2 * c'

Here is the header file:

Complex.h

#ifndef COMPLEX0_H
#define COMPLEX0_H

class complex {
    double realNum;
    double imagNum;
public:
    complex();
    complex(double x,double y);
    complex operator *(double n)const;
    complex operator *(const complex &c1)const;
   friend std::istream &operator>>(std::istream &is,complex &cm);
   friend std::ostream &operator<<(std::ostream &os,const complex &cm);
};

#endif

Here is the cpp:

Complex.cpp

 #include "iostream"
#include "complex0.h"

complex::complex() {
    imagNum = 0.0;
    realNum = 0.0;
}

complex::complex(double x, double y) {

    realNum = x;
    imagNum = y;
}

complex complex::operator *(const complex& c1) const{
complex sum;
sum.realNum=realNum*c1.realNum-c1.imagNum*imagNum;
sum.imagNum=realNum*c1.imagNum+imagNum*c1.realNum;
    return sum;
}

complex complex::operator *(double n)const{ 
    complex sum;
    sum.realNum=realNum*n;
    sum.imagNum=imagNum*n;
    return sum;

}
std::istream &operator >>(std::istream& is, complex& cm) {
    is >> cm.realNum>> cm.imagNum;
    return is;
}

std::ostream &operator <<(std::ostream& os, const complex& cm){
os<<"("<<cm.realNum<<","<<cm.imagNum<<"i)"<<"\n";    
return os;
}

main.cpp

#include <iostream>
using namespace std;
#include "complex0.h" 

    int main() {
        complex a(3.0, 4.0); 
        complex c;
        cout << "Enter a complex number (q to quit):\n";
        while (cin >> c) {
            cout << "c is " << c << "\n";
            cout << "a is " << a << "\n";
            cout << "a * c" << a * c << "\n";
            cout << "2 * c" << 2 * c << "\n";
            cout << "Enter a complex number (q to quit):\n";
        }
        cout << "Done!\n";
        return 0;
    }

Can someone explain to me what I have done wrong?

5
  • 1
    Because you only take complex on the left side, never a double. Commented Jul 17, 2013 at 19:18
  • The error I am getting is: no match for 'operator*' in '2 * c' Commented Jul 17, 2013 at 19:18
  • @chris what do you mean. Commented Jul 17, 2013 at 19:19
  • Every non-static function in your class has a first parameter of your class type. Commented Jul 17, 2013 at 19:22
  • Binary operators which do not assign should not be member functions. Commented Jul 17, 2013 at 19:27

2 Answers 2

3

The member function operator only applies when the first operand is of your class type. If you want to handle the case where the second operand is of your type, you need also a free function (in which we simply delegate to the member function by virtue of commutativity of the operation):

complex operator*(double n, complex const & x)
{
    return x * n;
}

(Please note that the standard library already contains <complex>.)

Sign up to request clarification or add additional context in comments.

Comments

0

You have a member function defined as follows:

complex complex::operator *(double n) const;

That will let you do things like: complex_number * 3.0, but not 3.0 * complex_number. However, you can't create a member function that will let you do 3.0 * complex_number. The only place you could create that member function, is inside the definition of double, which you can't change.

Instead of doing it as member functions though, you can also do it as free-standing functions:

complex operator*(complex x, double n); // Called for complex_number * 2.0
complex operator*(double n, complex x); // Called for 2.0 * complex_number

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.