[英]Error C2677: binary '*': no global operator found
Sorry for asking a similar question that has been asked before, but I wasn't able to understand the answers.很抱歉问了一个以前被问过的类似问题,但我无法理解答案。
Header file头文件
float Dot(const Point& other) const;
CPP CPP
Point Point::operator*(float operand) const
{
return Point(mX * operand, mY * operand);
}
and I am getting Error C2677: binary '*': no global operator found我收到错误 C2677: binary '*': no global operator found
what is the problem??问题是什么??
You have implemented operator*
in the cpp file but in the header file you only declared a simple Dot
function.您已经在 cpp 文件中实现了
operator*
,但在头文件中您只声明了一个简单的Dot
函数。
You either move the operator* inline in the header file or you add a declaration for it in the header file, ie:您可以在头文件中内联移动 operator* 或在头文件中为其添加声明,即:
Class Point {
[...]
Point operator*( float operand ) const {
return Point( mX * operand, mY * operand) );
}
};
That operator defined that way is a member operator because you have declared inside the class itself.以这种方式定义的运算符是成员运算符,因为您已在类本身内部声明。 You might also want to add a global operator , for example a comparison operator, which you will place outside your class declaration, for example:
您可能还想添加一个全局操作符,例如一个比较操作符,您将把它放在您的类声明之外,例如:
Class Point {
[...]
Point operator*( float operand ) const {
return Point( mX * operand, mY * operand) );
}
};
bool operator==(const Point& left, const Point& right)
{
return left.value == right.value;
};
Final thought:最后的想法:
So to recap the error you've got it most probably means that you didnt' declare the operator*
as global operator as shown above.因此,回顾一下您遇到的错误,很可能意味着您没有将
operator*
声明为全局运算符,如上所示。 Also I think you might confusing a dot product with a multiplication, (operator*), which held two very different results, also notice one is a scalar operation (in any dimension), the other is a Point, in this case, two dimension.另外,我认为您可能会将点积与乘法 (operator*) 混淆,它具有两个截然不同的结果,还要注意一个是标量运算(在任何维度中),另一个是点,在这种情况下,是二维.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.