error: no match for 'operator>>' (operand types are 'std::istream' {aka 'std::basic_istream<char>'} and 'Oper')

Advertisements

Task is to load complex number from a file. When code was written without classes, using stuctures and specified functions only it didn’t show that error. Functions (>> overlading) were completely the same.

Class

#ifndef WYRAZENIE_ZESPOLONE_HH
#define WYRAZENIE_ZESPOLONE_HH

#include "liczbaZespolona.hh"

enum Oper {op_plus, op_minus, op_razy, op_dziel};

class WyrazenieZespolone {
private:
    LiczbaZespolona lz1;
    LiczbaZespolona lz2;
    Oper op;

public:
    
    WyrazenieZespolone() = default;
    WyrazenieZespolone(const LiczbaZespolona, const LiczbaZespolona, Oper);

    LiczbaZespolona oblicz() const;

    friend std::ostream& operator << (std::ostream&, const WyrazenieZespolone&);
    friend std::istream& operator >> (std::istream&, WyrazenieZespolone&);
    friend std::istream& operator >> (std::istream&, Oper&);
};

#endif

Operators overloading implementation

std::istream& operator >> (std::istream& strm, WyrazenieZespolone& wz){
    strm >> wz.lz1 >> wz.op >> wz.lz2;

    return strm;
}

std::istream& operator >> (std::istream& strm, Oper& t_op){
    char znak;

    strm >> znak;
    switch(znak){
        case '+': {t_op = op_plus; break;} 
        case '-': {t_op = op_minus; break;}
        case '*': {t_op = op_razy; break;}
        case '/': {t_op = op_dziel; break;}
        default : {strm.setstate(std::ios::failbit);}
    }
    
    return strm;
}

I’m getting such error during compilation, even though vs code doesn’t show any mistakes

error: no match for 'operator>>' (operand types are 'std::istream' {aka 'std::basic_istream<char>'} and 'Oper')
     strm >> wz.lz1 >> wz.op >> wz.lz2;
     ~~~~~~~~~~~~~~~^~~~~~~~

>Solution :

The problem is that at the point when you wrote:

strm >> wz.lz1 >> wz.op >> wz.lz2;

compiler do not have the definition of the overloaded std::istream& operator >> (std::istream& strm, Oper& t_op) since it is defined afterwards.

So to solve this just move the definition of std::istream& operator >> (std::istream& strm, Oper& t_op) before the definition of std::istream& operator >> (std::istream& strm, WyrazenieZespolone& wz) as shown below. That is for strm >>wz.op to work, define the corresponding overloaded operator>> before its use.

Similarly, for strm >> wz.lz1 to work, define the corresponding overloaded operator>> before its use. This is shown in the below snippet Working demo.

//this comes first so that it can be used in strm >>wz.op
std::istream& operator >> (std::istream& strm, Oper& t_op){
    char znak;

    strm >> znak;
    switch(znak){
        case '+': {t_op = op_plus; break;} 
        case '-': {t_op = op_minus; break;}
        case '*': {t_op = op_razy; break;}
        case '/': {t_op = op_dziel; break;}
        default : {strm.setstate(std::ios::failbit);}
    }
    
    return strm;
}
//similarly this comes before so that it can be used in strm >> wz.lz1
std::istream& operator>>(std::istream& strm, LiczbaZespolona&)
{
    //do something here 
    return strm;
}
std::istream& operator >> (std::istream& strm, WyrazenieZespolone& wz){
    strm >> wz.lz1 >> wz.op >> wz.lz2;

    return strm;
}

Demo

Leave a ReplyCancel reply