简体   繁体   English

错误 C2677:二进制“*”:未找到全局运算符

[英]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.

相关问题 运算符重载:简单加法...错误C2677:二进制'+':找不到类型为___的全局运算符(或者没有可接受的转换) - Operator Overloading: Simple Addition… error C2677: binary '+': no global operator found with takes type ___ (or there is not acceptable conversion) 错误 C2677:二进制“+=”:未找到采用“电影”类型的全局运算符(或没有可接受的转换) - error C2677: binary '+=' : no global operator found which takes type 'Movie' (or there is no acceptable conversion) 枚举类型错误C2677 - Enum type error C2677 binary'==':没有运算符发现向量C ++的错误 - binary '==' : no operator found error for vector C++ c ++错误2679:二进制'>>':找不到运算符 - c++ error 2679: binary'>>' : no operator found 错误C2678:二进制'!=':找不到运算符 - error C2678: binary '!=' : no operator found C++ 二进制 '+'; 找不到类型为“Pair”的全局运算符 - C++ binary '+'; no global operator found which takes type 'Pair' 错误 C2679:二进制“>>”:未找到运算符 - error C2679: binary '>>' : no operator found 二进制'+ =':未找到类型为'Add'的全局运算符C ++ - Binary '+=' : no global operator found which takes type 'Add' C++ C ++编译器错误C2679:二进制'=':找不到运算符 - C++ Compiler Error C2679: binary '=' : no operator found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM