简体   繁体   English

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

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

I am trying to overload my operators its really just a class that holds arithmetic functions and a sequence of array variables. 我试图重载我的运算符它实际上只是一个包含算术函数和一系列数组变量的类。

But when i am overloading my (*) multiplication operator i get this error: 但是当我重载我的(*)乘法运算符时,我得到这个错误:

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

This happens when my code tries to do: s = 2*u; 当我的代码试图这样做时会发生这种情况: s = 2*u; in main.cpp 在main.cpp中

where s, and u are statistician classes. 其中s和u是统计学家。

statistician = my class 统计学家=我的班级

(statistician.h) (statistician.h)

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

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

..... more overloads...

};

Any help would be awesome thanks!! 任何帮助都会很棒,谢谢!!

Declare a namespace scope operator* , so that you can also have a convertible operand on the left hand side that is not of type statistician . 声明命名空间作用域operator* ,这样您也可以在左侧有一个不属于statistician类型的可转换操作数。

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

Needless to say that you should remove the in-class one then, and you need a converting constructor to take the int . 不用说你应该删除类中的那个,然后你需要一个转换构造函数来获取int

This is exactly why binary operators like * or + should be non-member. 这正是像*或+这样的二元运算符应该是非成员的原因。

If you did s = u * 2 , it would have worked, assuming that you have a non-explicit constructor for statistician that takes a single int argument. 如果你做了s = u * 2 ,它会有效,假设你有一个statistician的非显式构造函数,它接受一个int参数。 However, 2 * u does not work, because 2 is not a statistician , and int is not a class with a member operator* . 但是, 2 * u不起作用,因为2不是statistician ,而int不是具有成员operator*

For this to work right, you should define a non-member operator* and make it a friend of statistician : 为了使其正常工作,您应该定义一个非成员operator*并使其成为statisticianfriend


statistician operator*(const statistician &left, const statistician &right);

You also need to either define other versions of operator* that take integers (or whatever other types you wish to be able to "multiply") or define non-explicit constructors for statistician to enable implicit conversion. 您还需要定义其他版本的operator* ,它们采用整数(或您希望能够“乘法”的任何其他类型),或者为statistician定义非显式构造函数以启用隐式转换。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运算符重载:用二进制“-”减法没有找到采用_____类型的全局运算符(或没有可接受的转换) - Operator Overloading: subtraction with binary '-' no global operator found which takes type _____ (or there is no acceptable conversion) 错误 C2677:二进制“+=”:未找到采用“电影”类型的全局运算符(或没有可接受的转换) - error C2677: binary '+=' : no global operator found which takes type 'Movie' (or there is no acceptable conversion) 运算符重载:简单加法...错误C2677:二进制'+':找不到类型为___的全局运算符(或者没有可接受的转换) - Operator Overloading: Simple Addition… error C2677: binary '+': no global operator found with takes type ___ (or there is not acceptable conversion) 二进制'operator':未找到采用'Fraction'类型的右侧操作数的运算符(或没有可接受的转换) - Binary 'operator' : no operator found which takes a right-hand operand of type 'Fraction' (or there is no acceptable conversion) 二进制“==”:未找到采用“Enemy”类型左侧操作数的运算符(或没有可接受的转换) - binary '==': no operator found which takes a left-hand operand of type 'Enemy' (or there is no acceptable conversion) 二进制“ [”:找不到使用“初始化列表”类型的右侧操作数的运算符(或者没有可接受的转换) - binary '[': no operator found which takes a right-hand operand of type 'initializer list' (or there is no acceptable conversion) 二进制'=':未找到采用'_Ty'类型的左操作数的运算符(或没有可接受的转换) - binary '=': no operator found which takes a left-hand operand of type '_Ty' (or there is no acceptable conversion) C2679 二进制“-=”:未找到采用“T”类型右侧操作数的运算符(或没有可接受的转换) - C2679 binary '-=': no operator found which takes a right-hand operand of type 'T' (or there is no acceptable conversion) binary'==':找不到带有'std :: string'类型的左手操作数的运算符(或者没有可接受的转换) - binary '==' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion) C2678二进制'==':找不到使用'Card'类型的左操作数的运算符(或者没有可接受的转换) - C2678 binary '==': no operator found which takes a left-hand operand of type 'Card' (or there is no acceptable conversion)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM