简体   繁体   中英

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':

error C2677: binary '+': no global operator found which takes type 'RINT' (or there is not acceptable converstion)

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

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; should not change the value of a or b . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM