本文介绍了二进制'*':没有找到全局运算符,它接受类型“统计量”(或没有可接受的转换)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



但是当我重载我的(*)时,我会重载我的运算符它真的只是一个类,它保存算术函数和一系列数组变量。乘法运算符我得到这个错误:

 二进制'*':没有全局运算符找到类型'statistician'
(或没有可接受的转换)

这种情况发生在我的代码尝试做:<$ c $ main.cpp中的c> s = 2 * u;



其中s和u是统计学类。



statistician =我的班级



(statistician.h)

  class statistician 
{
...其他函数&变量...

const统计量statistician :: operator *(const statistician& other)const;

.....更多重载...

};

任何帮助都是真棒的感谢!!

解决方案

声明命名空间作用域 operator * ,以便您也可以在不是统计学家类型的左侧

 统计运算符*(const统计数据和左数,const统计数据与右数){
// ...
}

不用说你应该删除类中的需要一个转换构造函数来接受 int


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;in main.cpp

where s, and u are statistician classes.

statistician = my class

(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.

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.

这篇关于二进制'*':没有找到全局运算符,它接受类型“统计量”(或没有可接受的转换)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 07:16