How to implement operator overload << and >> in C++?

Asked

Viewed 566 times

2

Hello, I’m doing an exercise consisting of the following statement:

Enunciado do exercício

Sorry to put a print, but I could not select the text and it would be unnecessary to type everything here.

Finally, I made the class and implemented the overload of operators +, -, * and /, but I’m not able to do the part of operators << and >>, used in Cin and Cout.

    #include <iostream>
    using namespace std;

    class MeuInt{

    public:
        MeuInt(int i=0);
        int operator+(int);
        int operator-(int);
        int operator*(int);
        int operator*(MeuInt);
        int operator/(int);
        friend ostream &operator>>(ostream&, const MeuInt&);
        friend istream &operator>>(istream&, MeuInt&);

        int getInteiro();
        void setInteiro(int);

    private:
        int inteiro;
    };

    MeuInt::MeuInt(int i){
        inteiro = 1;
    }



    int MeuInt::operator+(int x){
        inteiro = x + inteiro;
        return inteiro;
    }
    int MeuInt::operator-(int x){
        inteiro = x - inteiro;
        return inteiro;
    }
    int MeuInt::operator*(int x){
        inteiro = x * inteiro;
        return inteiro;
    }

int MeuInt::operator*(MeuInt x){
    inteiro = x * inteiro;
    return inteiro;
}


int MeuInt::operator/(int x){
    inteiro = x / inteiro;
    return inteiro;
}

int MeuInt::getInteiro(){
    return inteiro;
}

void MeuInt::setInteiro(int x){
    inteiro = x;
}

MeuInt calcDelta(MeuInt a, MeuInt b, MeuInt c){
    MeuInt delta = b*b - a * c * 4;
    return delta;   
}

ostream &operator>>(ostream& saida, const MeuInt& meuint){
    output << meuint.getInteiro();
    return output;
}

istream &operator>>(istream& entrada, MeuInt& meuint){

}

int main(int argc, char** argv) {

    MeuInt inteiro1(10);
    MeuInt A, B, C;

    int c = inteiro1 + 1;

    cout << "inteiro1 + 1: ";
    cout << c << endl;

    cout << "Entre com as variáveis: " << endl;
    cout << "A: ";
    cin >> A;

    cout << "B: ";
    cin >> B;

    cout << "C: ";
    cin >> C;

    return 0;
}

With the above code, the IDE is returning the following errors:

  • In function 'Std::ostream & Operator>>(Std::ostream&, const Meuint&)':
  • 'output' not declared previously
  • Passing 'const Meuint' as 'this' argument from 'int Meuint::getInteiro()' discards qualifiers
  • In function 'Std::istream & Operator>>(Std::istream&, Meuint&)':
  • This function is not void and no return expression has been found

Thank you in advance.

  • have you ever tried to fix errors ? at least the first one is self-explanatory: "output' has not been declared previously"...

  • Yes, this was obvious and I ended up putting it without realizing it. I solved the errors and put the code in a reply. Thank you.

2 answers

2

In the function header: ostream &operator>>(ostream& saida, const MeuInt& meuint) Try to change operator >> for <<, for this is what is used in output operations. The problem 'output' não foi declarado anteriormente, is because you passed the type parameter ostream& with the name saida, and in the function you are using with the name output. Already in office istream& operator>>(std::istream&, MeuInt&), the problem as you said is because the function is not void, so you must return an element, in case an insertion operator implementation istream& entrada.

  • Yes, after a while looking at the code I realized I was >> instead of <, it happens... Now I managed to solve the problems, thank you.

1


I solved the above problems, so I leave the code here in case someone needs it one day. Just need to implement the methods that return the roots of the equation, I’m trying to do this.

#include <iostream>
using namespace std;

class MeuInt{

    public:
        MeuInt(int i=0);
        int operator+(int);
        int operator-(int);
        int operator*(int);
        int operator*(MeuInt);
        int operator/(int);


        int getInteiro();
        void setInteiro(int);

    private:
        int inteiro;
        friend ostream &operator<<(ostream&, const MeuInt&);
        friend istream &operator>>(istream&, MeuInt&);
};

MeuInt::MeuInt(int i){
    inteiro = 1;
}



int MeuInt::operator+(int x){
    inteiro = x + inteiro;
    return inteiro;
}
int MeuInt::operator-(int x){
        inteiro = x - inteiro;
    return inteiro;
}
int MeuInt::operator*(int x){
    inteiro = x * inteiro;
    return inteiro;
}

int MeuInt::operator*(MeuInt x){
    inteiro = x * inteiro;
    return inteiro;
}


int MeuInt::operator/(int x){
    inteiro = x / inteiro;
    return inteiro;
}

int MeuInt::getInteiro(){
    return inteiro;
}

void MeuInt::setInteiro(int x){
    inteiro = x;
}

MeuInt calcDelta(MeuInt a, MeuInt b, MeuInt c){
    MeuInt delta = b*b - a * c * 4;
    return delta;

}

ostream &operator<<(ostream& out, const MeuInt& meuint){
    out << meuint.inteiro;

    return out;
}

istream &operator>>(istream& in, MeuInt& meuint){

    in >> meuint.inteiro;

    return in;
}



int main(int argc, char** argv) {

    MeuInt inteiro1(10);
    MeuInt A, B, C;

    int c = inteiro1 + 1;

    cout << "inteiro1 + 1: ";
    cout << c << endl;

    cout << "Entre com as variáveis: " << endl;
    cout << "A: ";
    cin >> A;

    cout << "B: ";
    cin >> B;

    cout << "C: ";
    cin >> C;


    return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.