0
votes

Not able to understand why getting error

error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream’} and ‘MyStruct’) cout << st;

in the below code

#include <iostream>
using namespace std;

struct MyStruct
{
    int a;
    string b;
    double c;
    
    MyStruct(int a, string b, double c):a(a),b(b),c(c){}
    
    ostream& operator<<(ostream& os)
    {
        os << "a " << a << " b" << b << " c" << c;
        return os;
    }
};

template <typename T, typename ... Args>
T create(Args&& ... Arg)
{
    return T(Arg...);
}

int main() {
    // your code goes here
    MyStruct st = create<MyStruct>(5, "My Struct", 2.5);
    cout << st;
    return 0;
}
1
You define an << where you can do st << cout. - Some programmer dude

1 Answers

4
votes

The correct signature for the operator is:

    friend ostream& operator<<(ostream& os, const MyStruct& dt);

Right now your output stream operator is only overloaded in a way that allows st << cout.