二进制'*':找不到全局运算符,它接受类型'统计者'(或者没有可接受的转换)

Dar*_*val 2 c++ operator-overloading

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

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

     binary '*' : no global operator found which takes type 'statistician' 
(or there is no acceptable conversion)
Run Code Online (Sandbox Code Playgroud)

当我的代码尝试执行此操作时:s = 2*u;在main.cpp中

其中s和u是统计学家.

统计学家=我的班级

(statistician.h)

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

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

..... more overloads...

};
Run Code Online (Sandbox Code Playgroud)

任何帮助都会很棒,谢谢!!

Joh*_*itb 5

声明一个命名空间作用域operator*,这样你也可以在左侧有一个不属于类型的可转换操作数.statistician

statistician operator*(const statistician &left, const statistician &right) {
  // ...
}
Run Code Online (Sandbox Code Playgroud)

毋庸置疑,你应该删除那个类中的那个,然后你需要一个转换构造函数来获取int.