When I try to run the code, I get:
asdman@asdman-laptop:~$ g++ op.cpp op.cpp: In function ‘int main()’: op.cpp:21: error: no match for ‘operator+=’ in ‘c += operator+(((Vector&)(& a)), ((Vector&)(& a)))’ op.cpp:8: note: candidates are: void operator+=(Vector&, Vector&)And here's the actual source code:
#include <iostream>
struct Vector {
int x,y;
};
// Prototypes
void operator+=(Vector &a, Vector &b);
Vector operator+(Vector &a, Vector &b);
int main() {
Vector a={ 10,-2 };
Vector b={1,4};
std::cout << "Testing operator overloading with vectors." << std::endl;
std::cout << "Vector a is at (" << a.x << "," << a.y << ")." << std::endl;
std::cout << "Vector b is at (" << b.x << "," << b.y << ")." << std::endl;
Vector c={0,0}; c += (a+a); // <- This does not seem to work.
std::cout << "a + a equals in c, which is at (" << c.x << "," << c.y
<< ")." << std::endl;
return 0;
}
void operator+=(Vector &a, Vector &b) {
a.x +=b.x;
a.y+=b.y;
}
Vector operator+(Vector &a, Vector &b){
Vector c; // A return vector;
c.x = a.x+b.x;
c.y = a.y+b.y;
return c;
}
However, I am not entirely sure whether or not the actual problem lies in the struct or in the overloading part. Actually, I am completely clueless.
Any help appreciated! :)












