E0349 no operator "<<" matches these operands

never_ever

I try to overload operator << and ++ (post and pre). This is part of my code, but I get error "e0349: no operator matches these operands". Could you tell me where I made a mistake? (C++, VS2022)

#include <iostream>
#include <string>

using namespace std;

class K {
    int x, y;
public:
    K(int a, int b) :x(a), y(b) {};
    K() :x(0), y(0) {};
    K operator++(int);
    K& operator++();
    friend ostream& operator<< (ostream & str, K & obj);
    
};

K K::operator++(int) {
    K temp(*this);
    x += 1;
    y += 1;
    return temp;
}
K& K::operator++() {
    x += 1;
    y += 1;
    return *this;
}
ostream& operator<<(ostream& str, K& obj) {
    str << "[" << obj.x << ", " << obj.y << "]";
    return str;
}


int main(int argc, char* argv[])
{
    K obj{10, 20};
    cout << obj++ << endl;  //here I get error
    cout << obj << endl;
    cout << ++obj << endl;

}
StellarClown

Simply the post-operator++ is written so that it returns a copy of the temporary value which can be used as rvalue but being that the signature of the insertion operator requires a reference of the value, it does not know how to retrieve the data passed as a copy. This is how you should modify the overloading function of the extract operator:

ostream& operator<<(ostream& str, K const& obj) {
    str << "[" << obj.x << ", " << obj.y << "]";
    return str;
}

You're simply telling the function that the passed value will have a constant reference that it won't change over time. So it is as if you are taking the reference of the copy value. I know it's very tricky as a thing but I should have cleared your minds enough

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

E0349 no operator ">>" matches these operands

C++) E0349 no operator matches these operands occured

moving data from a 2D vector to a variable --> E0349 no operator "=" matches these operands

error: no operator "<" matches these operands

IntelliSense: no operator "<<" matches these operands

Error: no operator "==" matches these operands

no operator "/" matches these operands

no operator ""<<"" matches these operands error

Error : no operator " != " matches these operands

no operator "=" matches these operands error

"no operator >> matches these operands"

C++ No operator [] matches these operands

No operator "<<" matches these operands - C++

No operator "[ ]" matches these operands C++

No Operator = Matches Operands - DX11

C++ - Error - No operator "[]" matches these operands

more than one operator "[]" matches these operands

no operator "<<" matches these operands recursive tower of hanoi error

No operator "<" matches these operands operand types are: double < my_class

Accessing an entry in std::map causes 'no operator "[]" matches these operands'

C++ - no operator "<<" matches these operands directory_iterator()

No operator ">=" matches these operands error in c++ when using while loop

No operator "+" matches these operands, aka cannot add Array<double> to double

No Operator ">>" matches these operands operand types are: std::istream>>int

no operator ">>" matches these operands -- operand types are: std::istream >> const double

no operator ">>" matches these operands operand types are: std::istream >> double*

no operator matches these operands; operand types are: std::istream >> const char [5]

No operator "<<" matches these operands error between an object and a string literal

Error during implementation of operator>> :C++ no operator matches these operands operand types are: std::istream >> const double error