Const variable operation error

I've created a class of complex numbers and defined operations. I've also defined the imaginary unit i as constant in a namespace. When I write

1
2
3
complex z(0,0)
z = z + 2 + i
z = i

Everything works fine. But when I write z = 2 + i , the compiler gives me the error saying Invalid operands to binary expressions ('const complex' and 'int').

Where is my error?

EDIT :
Here is the implementation of the = and + operators in complex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void complex::operator=(const complex& other){
    realPart = other.realPart;
    imaginaryPart = other.imaginaryPart;
}
void complex::operator=(const double& other){
    realPart = other;
    imaginaryPart = 0;
}
complex complex::operator+(const complex& other){
    complex c = other;
    return complex(realPart + c.realPart, imaginaryPart + c.imaginaryPart);
}
complex complex::operator+(const double& other){
    return operator+(complex(other,0));
}

Last edited on
You'll have to post the whole error and your complex class for us to help you more.
Edited the post with the implementation of the operators
You have no operator defined for operator + (int, complex). Hence, the compiler doesn't know what to do with 2 + i.
Topic archived. No new replies allowed.