这是在cplusplus.com上找到的示例代码,用于重载操作符。我不确定“CVector(){};”意味着这里。它看起来不像一个构造函数,当我删除这一行时,编译器给我错误。类名后加空括号和空花括号
// overloading operators example
#include <iostream>
using namespace std;
class CVector {
public:
int x,y;
CVector() {}; <-- This Line
CVector (int a,int b) : x(a), y(b) {}
CVector operator + (const CVector&);
};
CVector CVector::operator+ (const CVector& param) {
CVector temp;
temp.x = x + param.x;
temp.y = y + param.y;
return temp;
}
int main() {
CVector foo (3,1);
CVector bar (1,2);
CVector result;
result = foo + bar;
cout << result.x << ',' << result.y << '\n';
return 0;
}
这是一个默认的构造函数。你不需要分号。另外,您的运营商+理想情况下不应该是会员功能。和cplusplus.com不是一个很好的学习资源。 –
必须有一个副本,但我找不到一个... – SergeyA
最好用初始化列表填写'x'和'y' –