0

I the next simple constructor:

Vector(double x=0, double y=0) : x(x), y(y) {}

void operator+=(const Vector& other) {
    this->x += other.x;
    this->y += other.y;
}

But when I call it like this

Vector b();
Vector a(1,1);

and try to do a += b; compiler gives me a lot of errors saying that no operators exist. However when I do like this: Vector b(0,0); or Vector b(0); everything works(((

6
  • Why would you think that such a constructor would enable usage of +=? If you want the operator, you have to overload it. That has nothing to do with constructors. But as a side note, you should make this constructor explicit, or simple numbers will implicitly convert to vectors as x coordinates. Commented Sep 23, 2014 at 13:54
  • I have an operator. Now I add it... Commented Sep 23, 2014 at 13:56
  • btw, this-> is not necessary Commented Sep 23, 2014 at 14:00
  • @RobertMutke I do like to use this in such cases to be clear which of the two objects is being used. Commented Sep 23, 2014 at 14:02
  • @NeilKirk kinda matter of taste, but still should be rather avoided stackoverflow.com/questions/2337540/… Commented Sep 23, 2014 at 14:09

1 Answer 1

3

Vector b(); doesn't create an object. It declares a function.

Vector b;
Vector a(1,1);

should work.

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

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.