Weird Operator overloading problem.

Hi all, I'm having this really strange problem with operator overloading, I copied it from a tutorial that was done on windows, they didn't seem to have a problem, I think it might be compiler specific.

It is a program to multiply, add and subtract vectors;

What really makes it wierd is the way that, for example:

//this works
Vector vect4 = vect3 * 2;

/*this does not
Vector vect4;
vect4 = vect3 * 2;
*/


I'm using g++.

http://pastebin.com/LWzJiWBh

_N
You need an operator= for your Vector class.

When you do :

Vector vect4;
vect4 = vect3 * 2;

The first line constructs vect4,
The second line attempts to change it's value using the operator=

However when you do:

Vector vect4 = vect3 * 2;

This works because you're creating vect4, and so your constructor gets called not operator=
- You can't bind a temporary to a non-const reference.
a temporary is returned from your operations, now check the assignment operator
Point operator=(Point &p)

- You shouldn't code unnecessary things.
The assignment operator provided by the compiler was good enough.
Actually, it was better than your attempt as it worked with temporaries

- You ought to respect const-correctness
If a function would not modify the object, you should mark it as `const', otherwise you can't use a function with a constant object
Topic archived. No new replies allowed.