我正在尝试重载我的运算符,它实际上只是一个包含算术函数和一系列数组变量的类。
但是当我重载我的 (*) 乘法运算符时,我得到这个错误:
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/