c++ - 重载运算符参数太多,visual studio c++

标签 c++ visual-studio

我试图在头文件中声明一个重载、非友元、非成员 ' - - 运算符:

 Quad operator-(const Quad &qu1, const Quad &qu2); 

但我得到:

 "error C2804: binary 'operator -' has too many parameters" 

这段代码直接来自书本和问题陈述,我似乎无法解决它。感谢您的帮助。

最佳答案

类定义范围内的二元运算符必须只有一个参数。

Quad operator-(const Quad &quRight)
{
    Quad res;
    res.x = this->x - quRight.x;
    // all other components
    // ...
   return res;
}

或者您可以将运算符重载移到类之外。

关于c++ - 重载运算符参数太多,visual studio c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33469438/

相关文章:

c - Microsoft visual studio 2010 是否支持 c99?

c++ - 为什么 std::get<T> 其中 T 是调用 constexpr 函数的结果失败?

c++ - "detail"和 "impl"文件夹有什么用?

c++ - 文件处理 C++ :Which is the ideal stream to be used while working with database files?

c++ - Visual Studio 中解决方案范围的构建配置?

c# - 如何以编程方式调整 PropertyGrid 控件的水平分隔线?

c++ - 无法访问对象 vector 中的成员函数

c++ - 在 C++ 中使用泰勒级数计算 Pi 的函数

git - Visual Studio 中分离的 HEAD

vb.net - 更改 ListView 子项的前景色