具有默认参数的类构造函数

时间:2014-09-23 13:52:11

标签: c++ class 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;
}

但是当我这样称呼它时

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

并尝试做+ = b; 编译器给了我很多错误,说没有运算符。但是当我喜欢这样的时候: 矢量b(0,0); 要么 矢量b(0); 一切正常((

1 个答案:

答案 0 :(得分:3)

Vector b();没有创建对象。它声明了一个函数。

Vector b;
Vector a(1,1);

应该有用。