C++重载+运算符会导致错误

daf*_*ffa 2 c++

我试图为我的类重载"+"运算符:

Vector operator+( const Vector& a, const Vector& b );
Run Code Online (Sandbox Code Playgroud)

但是,它告诉我:

vector.h(12): error C2804: binary 'operator +' has too many parameters
Run Code Online (Sandbox Code Playgroud)

我真的不明白.请帮我

ken*_*ytm 10

如果在类中定义运算符,则它应该只接收1个参数.

class Vector {
  ...
  Vector operator+ (const Vector& other) const {
    Vector res = *this;
    res += other;
    return res;
  }
  ...
};
Run Code Online (Sandbox Code Playgroud)

如果在类定义之外定义它,则使用2参数版本.

class Vector {
 ...
};

Vector operator+ (const Vector& first, const Vector& second) {
  Vector res = first;
  res += second;
  return res;
}
Run Code Online (Sandbox Code Playgroud)