简体   繁体   English

运算符重载:简单加法...错误C2677:二进制'+':找不到类型为___的全局运算符(或者没有可接受的转换)

[英]Operator Overloading: Simple Addition… error C2677: binary '+': no global operator found with takes type ___ (or there is not acceptable conversion)

Here is my header: 这是我的标题:

//RINT.h
#ifndef _RINT_H_
#define _RINT_H_

#include <iostream>

class RINT
{
    public:
        RINT();
        RINT(int);
        RINT(int, int);
        RINT operator+(RINT);
        RINT operator+(int);
        RINT operator+();
        friend std::ostream &operator<<(std::ostream &, const RINT &);
        friend std::istream &operator>>(std::istream &, RINT &);
    private:
        int a, b;
};

#endif

And the definitions: 以及定义:

//RINT.cpp
#include <iostream>
using namespace std;

#include "RINT_stack.h"

RINT::RINT()
{
    a = b = 0;
}

RINT::RINT(int x)
{
    a = x;
    b = 0;
}

RINT::RINT(int x, int y)
{
    a = x;
    b = y;
}

RINT RINT::operator+(RINT x)
{
    a += x.a;
    b += x.b;
    return RINT(a,b);
}

RINT RINT::operator+(int x)
{
    a += x;
    return RINT(a,b);
}

RINT RINT::operator+()
{
    return RINT(a,b);
}

ostream &operator<<(ostream &o, const RINT &x)
{
    o << x.a;
    o << x.b;
    return o;
}

istream &operator>>(istream &i, RINT &x)
{
    i >> x.a;
    i >> x.b;
    return i;
}

And finally, the test code: 最后,测试代码:

//RINT_test.cpp
#include "stdafx.h"
#include "RINT_stack.h"

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int main()
{
    RINT x, y = 4;
    int a = 5, b = 2;
    RINT z = y;

    x = 5;
    y = 6;
    z = x + y;
    z = x + 10;
    z = 1 + x; //error here!
    x = 1;
    x = +x;

    return 0;
}

I'm receiving the following error in RINT_test.cpp on line 20 at 'z = 1 + x': 我在“ z = 1 + x”的第20行的RINT_test.cpp中收到以下错误:

error C2677: binary '+': no global operator found which takes type 'RINT' (or there is not acceptable converstion) 错误C2677:二进制'+':未找到采用'RINT'类型的全局运算符(或没有可接受的对话)

I know that the error is coming in because the operator is preceded by an integer, but I'm not sure how to proceed from here. 我知道错误即将到来,因为运算符前面有一个整数,但是我不确定如何从此处继续。 Any help or direction is appreciated. 任何帮助或指导表示赞赏。 Thanks! 谢谢!

RINT RINT::operator+(RINT x)
{
    a += x.a;
    b += x.b;
    return RINT(a,b);
}

This isn't const, modifies the object you call it on, and thus can't be invoked on a temporary. 这不是const,会修改您在其上调用的对象,因此无法在临时对象上调用。 So if you're expecting z = 1 + x to create a temporary RINT from the 1 and then invoke operator+ on it, it can't. 因此,如果您期望z = 1 + x1创建一个临时RINT ,然后在其上调用operator+ ,则不能。

You want: 你要:

RINT RINT::operator+(RINT const& x) const
{
    return RINT(a + x.a, b + x.b);
}

You have similar bugs in other operators. 您在其他运算符中也有类似的错误。 Operators like + should not modify the object you invoke them on. 像运营商+ 应该修改调用它们的对象。 Code like c = a + b; c = a + b;代码c = a + b; should not change the value of a or b . 不应更改ab的值。 Only operators like += should do that. 只有像+=这样的运算符才可以这样做。

I know that the error is coming in because the operator is preceded by an integer, but I'm not sure how to proceed from here. 我知道错误即将到来,因为运算符前面有一个整数,但是我不确定如何从此处继续。

Yes, the compiler is right! 是的,编译器是正确的! You are missing global 缺少 全球

RDINT operator+(const int&, const RDINT&);

or 要么

int operator+(const int&, const RDINT&);

declaration/definition! 声明/定义!

You might notice, the 1st parameters of the function signature samples above match the preceeding integer from your mentioned code samples. 您可能会注意到,上面的函数签名示例的第一个参数与您提到的代码示例中的前面的整数匹配。

暂无
暂无

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

相关问题 错误 C2677:二进制“+=”:未找到采用“电影”类型的全局运算符(或没有可接受的转换) - error C2677: binary '+=' : no global operator found which takes type 'Movie' (or there is no acceptable conversion) 错误 C2677:二进制“*”:未找到全局运算符 - Error C2677: binary '*': no global operator found 运算符重载:用二进制“-”减法没有找到采用_____类型的全局运算符(或没有可接受的转换) - Operator Overloading: subtraction with binary '-' no global operator found which takes type _____ (or there is no acceptable conversion) 二进制&#39;*&#39;:找不到全局运算符,它接受类型&#39;统计者&#39;(或者没有可接受的转换) - binary '*' : no global operator found which takes type 'statistician' (or there is no acceptable conversion) 错误C2678:二进制'==':找不到哪个运算符采用类型的左操作数(或者没有可接受的转换) - error C2678: binary '==' : no operator found which takes a left-hand operand of type (or there is no acceptable conversion) 错误C2678:“ ==”二进制文件:未找到采用“ CSchemaString”类型的左操作数的运算符(或没有可接受的转换) - error C2678: '==' binary: no operator found which takes a left operand of type 'CSchemaString' (or there is no acceptable conversion) 枚举类型错误C2677 - Enum type error C2677 错误2错误C2679:二进制&#39;/&#39;:找不到哪个操作符采用类型的右操作数(或者没有可接受的转换) - Error 2 error C2679: binary '/' : no operator found which takes a right-hand operand of type (or there is no acceptable conversion) c ++错误c2679:二进制&#39;[&#39;:未找到采用&#39;SalesItem&#39;类型的右侧操作数的运算符(或者没有可接受的转换) - c++ error c2679: binary '[' : no operator found which takes a right-hand operand of type 'SalesItem' (or there is no acceptable conversion) 错误 C2679:二进制“&lt;&lt;”:未找到采用“mystring”类型的右侧操作数的运算符(或没有可接受的转换) - error C2679: binary '<<': no operator found which takes a right-hand operand of type 'mystring' (or there is no acceptable conversion)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM