我想通过重载 + 运算符来添加 2 个对象,但我的编译器说没有匹配的函数可以调用 point::point(int, int)。有人可以帮我处理这段代码,并解释错误吗?谢谢你
#include <iostream>
using namespace std;
class point{
int x,y;
public:
point operator+ (point & first, point & second)
{
return point (first.x + second.x,first.y + second.y);
}
};
int main()
{
point lf (1,3)
point ls (4,5)
point el = lf + ls;
return 0;
}
最佳答案
你可以像这样改变你的代码,
#include <iostream>
using namespace std;
class point {
int x, y;
public:
point(int i, int j)
{
x = i;
y = j;
}
point operator+ (const point & first) const
{
return point(x + first.x, y + first.y);
}
};
int main()
{
point lf(1, 3);
point ls(4, 5);
point el = lf + ls;
return 0;
}
希望这有助于...
关于加号运算符的 C++ 重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52376716/