no match for ‘operator<<’ in ‘std::operator
Solution 1:
You need to overload operator <<
for mystruct
class
Something like :-
friend ostream& operator << (ostream& os, const mystruct& m)
{
os << m.m_a <<" " << m.m_b << endl;
return os ;
}
See here
Solution 2:
There's only one error:
cout.cpp:26:29: error: no match for ‘operator<<’ in ‘std::operator<< [with _Traits = std::char_traits]((* & std::cout), ((const char*)"my structure ")) << m’
This means that the compiler couldn't find a matching overload for operator<<
. The rest of the output is the compiler listing operator<<
overloads that didn't match. The third line actually says this:
cout.cpp:26:29: note: candidates are:
Solution 3:
Obviously, the standard library provided operator does not know what to do with your user defined type mystruct
. It only works for predefined data types. To be able to use it for your own data type, You need to overload operator <<
to take your user defined data type.