0
I’m studying operator overload but my code is not compiling.
#include <iostream>
using namespace std;
class Complexo
{
public:
int real, image;
Complexo();
Complexo(int r, int i);
Complexo operator +(Complexo& c);
};
Complexo::Complexo(int r, int i) {
real = r;
image = i;
}
Complexo operator +(Complexo& c) {
Complexo x();
x.real = real + c.real;
x.image = image + c.image;
return x;
}
Complexo::Complexo() {};
int main() {
Complexo c1(1, 2);
Complexo c2(4, 4);
Complexo c3 = c1 + c2;
cout << "Parte real: " << c3.real << endl;
cout << "Parte imaginária: " << c3.image << endl;
return 0;
}
In the build, the error is as follows:
over.cpp: In function 'Complexo operator+(Complexo&)': over.cpp:25: error: 'real' was not declared in this scope over.cpp:26: error: 'image' was not declared in this scope
So it makes no sense to place prototype methods within a class? I have to necessarily put the whole body of function within the class so that the objects involved are accessed?
– Felipe Moura
The way you want it, yeah.
– Maniero