c++ - 二进制 '*' : no global operator found which takes type 'statistician' (or there is no acceptable conversion)

标签 c++ operator-overloading

我正在尝试重载我的运算符,它实际上只是一个包含算术函数和一系列数组变量的类。

但是当我重载我的 (*) 乘法运算符时,我得到这个错误:

     binary '*' : no global operator found which takes type 'statistician' 
(or there is no acceptable conversion)

当我的代码尝试执行以下操作时会发生这种情况:s = 2*u;in main.cpp

其中 s 和 u 是统计学家类别。

统计学家=我的类(class)

(统计学家.h)

class statistician  
{
... other functions & variables...

const statistician statistician::operator*(const statistician &other) const;

..... more overloads...

};

任何帮助都会非常感谢!!

最佳答案

声明一个命名空间作用域 operator*,这样您也可以在左侧 手边有一个非类型的可转换操作数统计学家

statistician operator*(const statistician &left, const statistician &right) {
  // ...
}

不用说,您应该删除类内的那个,并且您需要一个转换构造函数来获取 int

关于c++ - 二进制 '*' : no global operator found which takes type 'statistician' (or there is no acceptable conversion),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3588372/

相关文章:

c++ - C++ 中的条件运算符还能为我做什么?

c++ - 关于基类和派生类的构造函数调用

c++ - 诊断慢速链接

c++ - 具有流运算符重载的模板类

c++ - 隐式转换为指针

Pandas 中的 Python 运算符重载

c++ - 如何使用 std::vector<unsigned char*> 成员变量为类编写复制构造函数

c++ - 使用 glDrawArrays 绘制多个对象

c++ - 指向成员的指针的语法糖适用于数组但不适用于 std::vector

c++ - 如何在 `operator==` 包装类中使用重载 `std::variant` 来比较 Setting Vs Setting 和 T vs T?