Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
Here is the full program.
I'm using clang 13 with -std=c++20 -stdlib=libc++.
The error message, not included here because it's extremely long, basically says that the compiler does not even consider the operator<< function I've included here (error is on the std::cerr line near the end of the snippet).
#include <iostream>
#include <string>
#include <unordered_map>
template<typename K, typename V, typename ...Args>
std::ostream& operator<<(std::ostream& os, std::unordered_map<K, V, Args...> const& o) noexcept
{
for (auto const& [k, v] : o)
os << " " << k << ": " << v;
return os;
}
namespace my_namespace
{
struct A
{
std::unordered_map<int, std::string> map{{1, "hello"}, {2, "goodbye"}};
};
A a;
}
int main()
{
std::cerr << my_namespace::a; // error is here
}
Here is the lengthy error message (sorry for not including it earlier):
file.cpp:25:15: error: invalid operands to binary expression ('std::ostream' (aka 'basic_ostream<char>') and 'A')
std::cerr << a;
~~~~~~~~~ ^ ~
/usr/lib/llvm-13/bin/../include/c++/v1/cstddef:141:3: note: candidate function template not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'std::byte' for 1st argument
operator<< (byte __lhs, _Integer __shift) noexcept
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:748:1: note: candidate function template not viable: no known conversion from 'A' to 'char' for 2nd argument
operator<<(basic_ostream<_CharT, _Traits>& __os, char __cn)
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:781:1: note: candidate function template not viable: no known conversion from 'A' to 'char' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __os, char __c)
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:788:1: note: candidate function template not viable: no known conversion from 'A' to 'signed char' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __os, signed char __c)
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:795:1: note: candidate function template not viable: no known conversion from 'A' to 'unsigned char' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __os, unsigned char __c)
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:809:1: note: candidate function template not viable: no known conversion from 'A' to 'const char *' for 2nd argument
operator<<(basic_ostream<_CharT, _Traits>& __os, const char* __strn)
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:855:1: note: candidate function template not viable: no known conversion from 'A' to 'const char *' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __os, const char* __str)
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:862:1: note: candidate function template not viable: no known conversion from 'A' to 'const signed char *' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __os, const signed char* __str)
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:870:1: note: candidate function template not viable: no known conversion from 'A' to 'const unsigned char *' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __os, const unsigned char* __str)
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:1055:1: note: candidate function template not viable: no known conversion from 'A' to 'const std::error_code' for 2nd argument
operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __ec)
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:741:1: note: candidate template ignored: deduced conflicting types for parameter '_CharT' ('char' vs. 'A')
operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c)
^
/usr/lib/llvm-13/bin/../include/c++/v1/__random/uniform_int_distribution.h:282:1: note: candidate template ignored: could not match 'uniform_int_distribution<type-parameter-0-2>' against 'A'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:802:1: note: candidate template ignored: could not match 'const _CharT *' against 'A'
operator<<(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str)
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:1038:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'A'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:1046:1: note: candidate template ignored: could not match 'basic_string_view<type-parameter-0-0, type-parameter-0-1>' against 'A'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:1063:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-2>' against 'A'
operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p)
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:1082:1: note: candidate template ignored: could not match 'bitset<_Size>' against 'A'
operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x)
^
file.cpp:6:15: note: candidate template ignored: could not match 'unordered_map<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2...>' against 'A'
std::ostream& operator<<(std::ostream& os, std::unordered_map<K, V, Args...> const& o) noexcept
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:1030:11: note: candidate template ignored: requirement 'integral_constant<bool, false>::value' was not satisfied [with _Stream = std::ostream &, _Tp = A]
_Stream&& operator<<(_Stream&& __os, const _Tp& __x)
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:1075:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-2, type-parameter-0-3>' against 'A'
operator<<(basic_ostream<_CharT, _Traits>& __os, unique_ptr<_Yp, _Dp> const& __p)
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:188:20: note: candidate function not viable: no known conversion from 'A' to 'std::ostream &(*)(std::ostream &)' for 1st argument
basic_ostream& operator<<(basic_ostream& (*__pf)(basic_ostream&))
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:192:20: note: candidate function not viable: no known conversion from 'A' to 'basic_ios<std::basic_ostream<char>::char_type, std::basic_ostream<char>::traits_type> &(*)(basic_ios<std::basic_ostream<char>::char_type, std::basic_ostream<char>::traits_type> &)' (aka 'basic_ios<char, std::char_traits<char>> &(*)(basic_ios<char, std::char_traits<char>> &)') for 1st argument
basic_ostream& operator<<(basic_ios<char_type, traits_type>&
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:197:20: note: candidate function not viable: no known conversion from 'A' to 'std::ios_base &(*)(std::ios_base &)' for 1st argument
basic_ostream& operator<<(ios_base& (*__pf)(ios_base&))
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:200:20: note: candidate function not viable: no known conversion from 'A' to 'bool' for 1st argument
basic_ostream& operator<<(bool __n);
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:201:20: note: candidate function not viable: no known conversion from 'A' to 'short' for 1st argument
basic_ostream& operator<<(short __n);
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:202:20: note: candidate function not viable: no known conversion from 'A' to 'unsigned short' for 1st argument
basic_ostream& operator<<(unsigned short __n);
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:203:20: note: candidate function not viable: no known conversion from 'A' to 'int' for 1st argument
basic_ostream& operator<<(int __n);
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:204:20: note: candidate function not viable: no known conversion from 'A' to 'unsigned int' for 1st argument
basic_ostream& operator<<(unsigned int __n);
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:205:20: note: candidate function not viable: no known conversion from 'A' to 'long' for 1st argument
basic_ostream& operator<<(long __n);
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:206:20: note: candidate function not viable: no known conversion from 'A' to 'unsigned long' for 1st argument
basic_ostream& operator<<(unsigned long __n);
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:207:20: note: candidate function not viable: no known conversion from 'A' to 'long long' for 1st argument
basic_ostream& operator<<(long long __n);
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:208:20: note: candidate function not viable: no known conversion from 'A' to 'unsigned long long' for 1st argument
basic_ostream& operator<<(unsigned long long __n);
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:209:20: note: candidate function not viable: no known conversion from 'A' to 'float' for 1st argument
basic_ostream& operator<<(float __f);
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:210:20: note: candidate function not viable: no known conversion from 'A' to 'double' for 1st argument
basic_ostream& operator<<(double __f);
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:211:20: note: candidate function not viable: no known conversion from 'A' to 'long double' for 1st argument
basic_ostream& operator<<(long double __f);
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:212:20: note: candidate function not viable: no known conversion from 'A' to 'const void *' for 1st argument; take the address of the argument with &
basic_ostream& operator<<(const void* __p);
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:213:20: note: candidate function not viable: no known conversion from 'A' to 'basic_streambuf<std::basic_ostream<char>::char_type, std::basic_ostream<char>::traits_type> *' (aka 'basic_streambuf<char, std::char_traits<char>> *') for 1st argument
basic_ostream& operator<<(basic_streambuf<char_type, traits_type>* __sb);
^
/usr/lib/llvm-13/bin/../include/c++/v1/ostream:216:20: note: candidate function not viable: no known conversion from 'A' to 'std::nullptr_t' (aka 'nullptr_t') for 1st argument
basic_ostream& operator<<(nullptr_t)
^
1 error generated.
You have provided operator<< for a wrong type there. You gave for the std::unordered_map not for the my_namespace::A. Hence, the compiler can not find a operator<< for my_namespace::A.
You need instead
namespace my_namespace
{
// ...
std::ostream& operator<<(std::ostream& os, A const& a) noexcept
// ^^^^^^^^^^^^
{
for (auto const& [k, v] : a.map)
os << " " << k << ": " << v;
return os;
}
}
See demo
You overload operator<< for std::unordered_map<int, std::string> but not my_namespace::A here.
One way is to overload it for my_namespace::A.
std::ostream& operator<<(std::ostream& os, const A& a) noexcept
{
for (auto const& [k, v] : a.map)
os << " " << k << ": " << v;
return os;
}
Another way is to make A can be implicitly converted to std::unordered_map<int, std::string>.
struct A
{
A() {}
std::unordered_map<int, std::string> map{{1, "hello"}, {2, "goodbye"}};
operator std::unordered_map<int, std::string>() const {
return map;
}
};
I want to support ostream for std::optional<T>. My problem is std::optional<user defined type> is captured by the new function that I have added, but std::optional<int> is not captured.
Inside foo.h, I have the following code
template <typename T>
std::ostream& operator<<(std::ostream& stream, const std::optional<T>& info)
{
if (info) {
stream << *info;
} else {
stream << "None";
}
return stream;
}
Inside types.h:
struct MyType {
...
...
};
inline std::ostream& operator<<(std::ostream& stream, const MyType& my_type)
{
...
return stream;
}
Inside pqr.h
#include <"foo.h">
#include <"types.h">
struct ABC {
std::optional<int> a;
std::optional<MyType> b;
}
inline std::ostream& operator<<(std::ostream& stream, const ABC& abc)
{
stream << abc.a; //commenting this line solves the issue
stream << abc.b; //Printing userdefined structure has no issues
}
Commenting the line
stream << abc.a;
solves the issue. It is so weird that my template code is not captured for std::optional<int>, but captured for my user-defined type std::optional<MyType>. The even weirder part is that both the lines are next to each other in the same file.
COMPILATION LOG:
error: invalid operands to binary expression ('basic_ostream<char, std::__1::char_traits<char> >' and 'const std::optional<int>')
stream << abc.a;
~~~~^ ~~~~~~~
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:219:20: note: candidate function not viable: no known conversion from 'const std::optional<int>' to 'const void *' for 1st argument; take the address of the argument with &
basic_ostream& operator<<(const void* __p);
^
/usr/lib/llvm-9/bin/../include/c++/v1/type_traits:4034:3: note: candidate function template not viable: no known conversion from 'basic_ostream<char, std::__1::char_traits<char> >' to 'std::byte' for 1st argument
operator<< (byte __lhs, _Integer __shift) noexcept
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:195:20: note: candidate function not viable: no known conversion from 'const std::optional<int>' to 'std::__1::basic_ostream<char> &(*)(std::__1::basic_ostream<char> &)' for 1st argument
basic_ostream& operator<<(basic_ostream& (*__pf)(basic_ostream&))
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:199:20: note: candidate function not viable: no known conversion from 'const std::optional<int>' to 'basic_ios<std::__1::basic_ostream<char, std::__1::char_traits<char> >::char_type, std::__1::basic_ostream<char, std::__1::char_traits<char> >::traits_type> &(*)(basic_ios<std::__1::basic_ostream<char, std::__1::char_traits<char> >::char_type, std::__1::basic_ostream<char, std::__1::char_traits<char> >::traits_type> &)' (aka 'basic_ios<char, std::__1::char_traits<char> > &(*)(basic_ios<char, std::__1::char_traits<char> > &)') for 1st argument
basic_ostream& operator<<(basic_ios<char_type, traits_type>&
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:204:20: note: candidate function not viable: no known conversion from 'const std::optional<int>' to 'std::__1::ios_base &(*)(std::__1::ios_base &)' for 1st argument
basic_ostream& operator<<(ios_base& (*__pf)(ios_base&))
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:207:20: note: candidate function not viable: no known conversion from 'const std::optional<int>' to 'bool' for 1st argument
basic_ostream& operator<<(bool __n);
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:208:20: note: candidate function not viable: no known conversion from 'const std::optional<int>' to 'short' for 1st argument
basic_ostream& operator<<(short __n);
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:209:20: note: candidate function not viable: no known conversion from 'const std::optional<int>' to 'unsigned short' for 1st argument
basic_ostream& operator<<(unsigned short __n);
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:210:20: note: candidate function not viable: no known conversion from 'const std::optional<int>' to 'int' for 1st argument
basic_ostream& operator<<(int __n);
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:211:20: note: candidate function not viable: no known conversion from 'const std::optional<int>' to 'unsigned int' for 1st argument
basic_ostream& operator<<(unsigned int __n);
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:212:20: note: candidate function not viable: no known conversion from 'const std::optional<int>' to 'long' for 1st argument
basic_ostream& operator<<(long __n);
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:213:20: note: candidate function not viable: no known conversion from 'const std::optional<int>' to 'unsigned long' for 1st argument
basic_ostream& operator<<(unsigned long __n);
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:214:20: note: candidate function not viable: no known conversion from 'const std::optional<int>' to 'long long' for 1st argument
basic_ostream& operator<<(long long __n);
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:215:20: note: candidate function not viable: no known conversion from 'const std::optional<int>' to 'unsigned long long' for 1st argument
basic_ostream& operator<<(unsigned long long __n);
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:216:20: note: candidate function not viable: no known conversion from 'const std::optional<int>' to 'float' for 1st argument
basic_ostream& operator<<(float __f);
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:217:20: note: candidate function not viable: no known conversion from 'const std::optional<int>' to 'double' for 1st argument
basic_ostream& operator<<(double __f);
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:218:20: note: candidate function not viable: no known conversion from 'const std::optional<int>' to 'long double' for 1st argument
basic_ostream& operator<<(long double __f);
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:220:20: note: candidate function not viable: no known conversion from 'const std::optional<int>' to 'basic_streambuf<std::__1::basic_ostream<char, std::__1::char_traits<char> >::char_type, std::__1::basic_ostream<char, std::__1::char_traits<char> >::traits_type> *' (aka 'basic_streambuf<char, std::__1::char_traits<char> > *') for 1st argument
basic_ostream& operator<<(basic_streambuf<char_type, traits_type>* __sb);
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:223:20: note: candidate function not viable: no known conversion from 'const std::optional<int>' to 'std::nullptr_t' (aka 'nullptr_t') for 1st argument
basic_ostream& operator<<(nullptr_t)
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:760:1: note: candidate function template not viable: no known conversion from 'const std::optional<int>' to 'char' for 2nd argument
operator<<(basic_ostream<_CharT, _Traits>& __os, char __cn)
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:793:1: note: candidate function template not viable: no known conversion from 'const std::optional<int>' to 'char' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __os, char __c)
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:800:1: note: candidate function template not viable: no known conversion from 'const std::optional<int>' to 'signed char' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __os, signed char __c)
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:807:1: note: candidate function template not viable: no known conversion from 'const std::optional<int>' to 'unsigned char' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __os, unsigned char __c)
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:821:1: note: candidate function template not viable: no known conversion from 'const std::optional<int>' to 'const char *' for 2nd argument
operator<<(basic_ostream<_CharT, _Traits>& __os, const char* __strn)
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:867:1: note: candidate function template not viable: no known conversion from 'const std::optional<int>' to 'const char *' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __os, const char* __str)
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:874:1: note: candidate function template not viable: no known conversion from 'const std::optional<int>' to 'const signed char *' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __os, const signed char* __str)
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:882:1: note: candidate function template not viable: no known conversion from 'const std::optional<int>' to 'const unsigned char *' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __os, const unsigned char* __str)
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:1066:1: note: candidate function template not viable: no known conversion from 'const std::optional<int>' to 'const std::__1::error_code' for 2nd argument
operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __ec)
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:3895:1: note: candidate function template not viable: no known conversion from 'const std::optional<int>' to 'const std::__1::bernoulli_distribution' for 2nd argument
operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x)
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:753:1: note: candidate template ignored: deduced conflicting types for parameter '_CharT' ('char' vs. 'std::__1::optional<int>')
operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c)
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:814:1: note: candidate template ignored: could not match 'const _CharT *' against 'std::optional<int>'
operator<<(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str)
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:1049:1: note: candidate template ignored: could not match 'basic_string' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:1039:1: note: candidate template ignored: requirement '!is_lvalue_reference<std::__1::basic_ostream<char> &>::value' was not satisfied [with _Stream = std::__1::basic_ostream<char> &, _Tp = std::__1::optional<int>]
operator<<(_Stream&& __os, const _Tp& __x)
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:1057:1: note: candidate template ignored: could not match 'basic_string_view' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:1074:1: note: candidate template ignored: could not match 'shared_ptr' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p)
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:1086:1: note: candidate template ignored: could not match 'unique_ptr' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os, unique_ptr<_Yp, _Dp> const& __p)
^
/usr/lib/llvm-9/bin/../include/c++/v1/ostream:1093:1: note: candidate template ignored: could not match 'bitset' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x)
^
/usr/lib/llvm-9/bin/../include/c++/v1/valarray:4165:1: note: candidate template ignored: substitution failure [with _Expr1 = std::__1::basic_ostream<char>, _Expr2 = std::__1::optional<int>]: no type named 'value_type' in 'std::__1::basic_ostream<char>'
operator<<(const _Expr1& __x, const _Expr2& __y)
^
/usr/lib/llvm-9/bin/../include/c++/v1/valarray:4180:1: note: candidate template ignored: substitution failure [with _Expr = std::__1::basic_ostream<char>]: no type named 'value_type' in 'std::__1::basic_ostream<char>'
operator<<(const _Expr& __x, const typename _Expr::value_type& __y)
^
/usr/lib/llvm-9/bin/../include/c++/v1/valarray:4196:1: note: candidate template ignored: requirement '__is_val_expr<std::__1::optional<int> >::value' was not satisfied [with _Expr = std::__1::optional<int>]
operator<<(const typename _Expr::value_type& __x, const _Expr& __y)
^
/usr/lib/llvm-9/bin/../include/c++/v1/iomanip:362:1: note: candidate template ignored: could not match '__iom_t8' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x)
^
/usr/lib/llvm-9/bin/../include/c++/v1/iomanip:482:1: note: candidate template ignored: could not match '__iom_t10' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x)
^
/usr/lib/llvm-9/bin/../include/c++/v1/iomanip:572:33: note: candidate template ignored: could not match '__quoted_output_proxy' against 'optional'
basic_ostream<_CharT, _Traits>& operator<<(
^
/usr/lib/llvm-9/bin/../include/c++/v1/iomanip:592:33: note: candidate template ignored: could not match '__quoted_proxy' against 'optional'
basic_ostream<_CharT, _Traits>& operator<<(
^
/usr/lib/llvm-9/bin/../include/c++/v1/regex:5238:1: note: candidate template ignored: could not match 'sub_match' against 'optional'
operator<<(basic_ostream<_CharT, _ST>& __os, const sub_match<_BiIter>& __m)
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:1981:1: note: candidate template ignored: could not match 'linear_congruential_engine' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:2451:1: note: candidate template ignored: could not match 'mersenne_twister_engine' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:2772:1: note: candidate template ignored: could not match 'subtract_with_carry_engine' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:2954:1: note: candidate template ignored: could not match 'discard_block_engine' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:3207:1: note: candidate template ignored: could not match 'independent_bits_engine' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:3439:1: note: candidate template ignored: could not match 'shuffle_order_engine' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:3662:1: note: candidate template ignored: could not match 'uniform_int_distribution' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:3783:1: note: candidate template ignored: could not match 'uniform_real_distribution' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:4073:1: note: candidate template ignored: could not match 'binomial_distribution' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:4191:1: note: candidate template ignored: could not match 'exponential_distribution' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:4345:1: note: candidate template ignored: could not match 'normal_distribution' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:4495:1: note: candidate template ignored: could not match 'lognormal_distribution' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:4716:1: note: candidate template ignored: could not match 'poisson_distribution' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:4827:1: note: candidate template ignored: could not match 'weibull_distribution' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:4946:1: note: candidate template ignored: could not match 'extreme_value_distribution' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:5118:1: note: candidate template ignored: could not match 'gamma_distribution' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:5254:1: note: candidate template ignored: could not match 'negative_binomial_distribution' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:5360:1: note: candidate template ignored: could not match 'geometric_distribution' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:5464:1: note: candidate template ignored: could not match 'chi_squared_distribution' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:5584:1: note: candidate template ignored: could not match 'cauchy_distribution' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:5706:1: note: candidate template ignored: could not match 'fisher_f_distribution' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:5822:1: note: candidate template ignored: could not match 'student_t_distribution' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:6045:1: note: candidate template ignored: could not match 'discrete_distribution' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:6346:1: note: candidate template ignored: could not match 'piecewise_constant_distribution' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/lib/llvm-9/bin/../include/c++/v1/random:6685:1: note: candidate template ignored: could not match 'piecewise_linear_distribution' against 'optional'
operator<<(basic_ostream<_CharT, _Traits>& __os,
I have a program that compiles in XCode. In that program, I am taking advantage of stringstream's ability to store values according to the type that they are being assigned to.
Eg) The user inputs a letter, a number, and a symbol separated by spaces in the console. I make a call to getline that gets all three values and stores them in a string. I then create a stringstream with that string's data and assign the values to some variables, a char, an int, and a char in this case. I compile and run within XCode and this works. Then, I take that same file in a terminal and call g++ -o executableName fileName.cpp. Here, I get an error.
Working Example:
main.cpp
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
string buffer;
char letter;
int number;
char symbol;
cout << "Enter (letter) (number) (symbol): ";
getline(cin, buffer);
stringstream(buffer) >> letter >> number >> symbol; //The error occurs here
cout << letter << " " << number << " " << symbol << endl;
return 0;
}
I compile and run in XCode:
Enter (letter) (number) (symbol): B 145 #
B 145 #
Program ended with exit code: 0
I compile with g++:
$ g++ -o test main.cpp
main.cpp:14:26: error: invalid operands to binary expression ('std::__1::stringstream' (aka 'basic_stringstream<char>') and 'int')
stringstream(buffer) >> letter >> number >> symbol;
~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~
and then follows all of this:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:789:1: note: candidate function not viable: no known conversion from 'char' to 'unsigned char ' for 2nd
argument
operator>>(basic_istream& __is, unsigned char __s)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:797:1: note: candidate function not viable: no known conversion from 'char' to 'signed char ' for 2nd
argument
operator>>(basic_istream& __is, signed char __s)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:804:1: note: candidate function [with _CharT = char, _Traits = std::__1::char_traits] not viable:
no known conversion from 'std::__1::stringstream' (aka 'basic_stringstream') to 'basic_istream > &' for 1st argument
operator>>(basic_istream<_CharT, _Traits>& __is, _CharT& __c)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:832:1: note: candidate function not viable: no known conversion from 'char' to 'unsigned char &' for 2nd
argument
operator>>(basic_istream& __is, unsigned char& __c)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:840:1: note: candidate function not viable: no known conversion from 'char' to 'signed char &' for 2nd
argument
operator>>(basic_istream& __is, signed char& __c)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:220:20: note: candidate function not viable: no known conversion from 'char' to
'std::__1::basic_istream &()(std::__1::basic_istream &)' for 1st argument
basic_istream& operator>>(basic_istream& (__pf)(basic_istream&))
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:224:20: note: candidate function not viable: no known conversion from 'char' to
'basic_ios >::char_type, std::__1::basic_istream >::traits_type> &()(basic_ios >::char_type, std::__1::basic_istream >::traits_type> &)' (aka 'basic_ios > &()(basic_ios > &)') for 1st argument
basic_istream& operator>>(basic_ios&
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:229:20: note: candidate function not viable: no known conversion from 'char' to
'std::__1::ios_base &()(std::__1::ios_base &)' for 1st argument
basic_istream& operator>>(ios_base& (__pf)(ios_base&))
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:232:20: note: candidate function not viable: no known conversion from 'char' to
'basic_streambuf >::char_type, std::__1::basic_istream >::traits_type> *'
(aka 'basic_streambuf > ') for 1st argument
basic_istream& operator>>(basic_streambuf __sb);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:233:20: note: candidate function not viable: no known conversion from 'char' to 'bool &' for 1st argument
basic_istream& operator>>(bool& __n);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:234:20: note: candidate function not viable: no known conversion from 'char' to 'short &' for 1st
argument
basic_istream& operator>>(short& __n);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:235:20: note: candidate function not viable: no known conversion from 'char' to 'unsigned short &' for
1st argument
basic_istream& operator>>(unsigned short& __n);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:236:20: note: candidate function not viable: no known conversion from 'char' to 'int &' for 1st argument
basic_istream& operator>>(int& __n);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:237:20: note: candidate function not viable: no known conversion from 'char' to 'unsigned int &' for 1st
argument
basic_istream& operator>>(unsigned int& __n);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:238:20: note: candidate function not viable: no known conversion from 'char' to 'long &' for 1st argument
basic_istream& operator>>(long& __n);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:239:20: note: candidate function not viable: no known conversion from 'char' to 'unsigned long &' for 1st
argument
basic_istream& operator>>(unsigned long& __n);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:240:20: note: candidate function not viable: no known conversion from 'char' to 'long long &' for 1st
argument
basic_istream& operator>>(long long& __n);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:241:20: note: candidate function not viable: no known conversion from 'char' to 'unsigned long long &'
for 1st argument
basic_istream& operator>>(unsigned long long& __n);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:242:20: note: candidate function not viable: no known conversion from 'char' to 'float &' for 1st
argument
basic_istream& operator>>(float& __f);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:243:20: note: candidate function not viable: no known conversion from 'char' to 'double &' for 1st
argument
basic_istream& operator>>(double& __f);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:244:20: note: candidate function not viable: no known conversion from 'char' to 'long double &' for 1st
argument
basic_istream& operator>>(long double& __f);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:245:20: note: candidate function not viable: no known conversion from 'char' to 'void &' for 1st
argument
basic_istream& operator>>(void& __p);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:740:1: note: candidate template ignored: could not match '_CharT ' against 'char'
operator>>(basic_istream<_CharT, _Traits>& __is, _CharT __s)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:1506:1: note: candidate template ignored: could not match 'basic_string' against 'char'
operator>>(basic_istream<_CharT, _Traits>& __is,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:1638:1: note: candidate template ignored: could not match 'bitset<_Size>' against 'char'
operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x)
^
1 error generated.
Is my use of stringstream wrong? (as an aside, Why does XCode allow the code to compile while g++ does not? I thought XCode uses the same g++)
I find C++ provides the default operator<< function for enum type:
#include <iostream>
using namespace std;
enum OpType {
Select,
Insert
};
int main() {
OpType t = Select;
cout << t;
return 0;
}
Running result is:
0
While doesn't provide default operator>> function:
#include <iostream>
using namespace std;
enum OpType {
Select,
Insert
};
int main() {
OpType t = Select;
cin >> t;
return 0;
}
Build it will generate following compile errors:
prog.cpp: In function ‘int main()’:
prog.cpp:11:6: error: no match for ‘operator>>’ (operand types are ‘std::istream {aka std::basic_istream<char>}’ and ‘OpType’)
cin >> t;
~~~~^~~~
In file included from /usr/include/c++/6/iostream:40:0,
from prog.cpp:1:
/usr/include/c++/6/istream:168:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>] <near match>
operator>>(bool& __n)
^~~~~~~~
/usr/include/c++/6/istream:168:7: note: conversion of argument 1 would be ill-formed:
prog.cpp:11:9: error: invalid initialization of non-const reference of type ‘bool&’ from an rvalue of type ‘bool’
cin >> t;
^
In file included from /usr/include/c++/6/iostream:40:0,
from prog.cpp:1:
/usr/include/c++/6/istream:172:7: note: candidate: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char; _Traits = std::char_traits<char>] <near match>
operator>>(short& __n);
^~~~~~~~
/usr/include/c++/6/istream:172:7: note: conversion of argument 1 would be ill-formed:
prog.cpp:11:9: error: invalid initialization of non-const reference of type ‘short int&’ from an rvalue of type ‘short int’
cin >> t;
^
In file included from /usr/include/c++/6/iostream:40:0,
from prog.cpp:1:
/usr/include/c++/6/istream:175:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>] <near match>
operator>>(unsigned short& __n)
^~~~~~~~
/usr/include/c++/6/istream:175:7: note: conversion of argument 1 would be ill-formed:
prog.cpp:11:9: error: invalid initialization of non-const reference of type ‘short unsigned int&’ from an rvalue of type ‘short unsigned int’
cin >> t;
^
In file included from /usr/include/c++/6/iostream:40:0,
from prog.cpp:1:
/usr/include/c++/6/istream:179:7: note: candidate: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char; _Traits = std::char_traits<char>] <near match>
operator>>(int& __n);
^~~~~~~~
/usr/include/c++/6/istream:179:7: note: conversion of argument 1 would be ill-formed:
prog.cpp:11:9: error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’
cin >> t;
^
In file included from /usr/include/c++/6/iostream:40:0,
from prog.cpp:1:
/usr/include/c++/6/istream:182:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>] <near match>
operator>>(unsigned int& __n)
^~~~~~~~
/usr/include/c++/6/istream:182:7: note: conversion of argument 1 would be ill-formed:
prog.cpp:11:9: error: invalid initialization of non-const reference of type ‘unsigned int&’ from an rvalue of type ‘unsigned int’
cin >> t;
^
In file included from /usr/include/c++/6/iostream:40:0,
from prog.cpp:1:
/usr/include/c++/6/istream:186:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>] <near match>
operator>>(long& __n)
^~~~~~~~
/usr/include/c++/6/istream:186:7: note: conversion of argument 1 would be ill-formed:
prog.cpp:11:9: error: invalid initialization of non-const reference of type ‘long int&’ from an rvalue of type ‘long int’
cin >> t;
^
In file included from /usr/include/c++/6/iostream:40:0,
from prog.cpp:1:
/usr/include/c++/6/istream:190:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>] <near match>
......
Why doesn't C++ provide default operator>> function for enum type?
Even disregarding the fact that there's no << operator either — the enum is implicitly converted to an integer first — it's not obvious how it could be standardised.
Would the result of reading an enum value that doesn't exist be an error, unspecified, implementation-defined, or undefined?
Different applications will want different behaviour, perhaps even within the same application.
And that puts everyone back at square one, writing the same code as we do now.
There's also the complication that the stream operators aren't part of the language itself, and neither is the stream concept, so you can't really have the compiler generate them for you.
There is no operator << taking enum, you are calling an overload taking integer with implicit conversion of your enum value to integer. The reason for this is that operator << and operator >> overloads taking integers (and other inputs) are just normal functions, not built-in operators. Supplying generic method for user-defined types (such as enums) is not possible right now as it will require compile-time reflection of some kind.
I'm when trying to write to a stringstream using a overload of << for ostream gcc fails with:
error: no match for 'operator<<' (operand types are 'std::stringstream&() {aka std::__cxx11::basic_stringstream&()}' and 'const Test')
As far as I can tell the operator is defined, I've used similar code with stringstream (input operator for other classes where I overload for istream) with out any problems using the same compiler.
Example Code:
// Header
#include <istream>
#include <ostream>
class Test {
public:
Test* Clone() const { return onClone(); }
friend std::istream& operator >> (std::istream& lhs, Test& rhs) { rhs.onLoad(lhs); return lhs; }
friend std::ostream& operator << (std::ostream& lhs, const Test& rhs) { rhs.onSave(lhs); return lhs; }
private:
virtual void onLoad(std::istream& in) {}
virtual void onSave(std::ostream& out) const {}
Test* Test::onClone() const;
};
// Source
#include <sstream>
Test* Test::onClone() const
{
Test * tmp = new Test();
std::stringstream& buf();
buf << (*this); //!< ERROR HERE
buf >> (*tmp);
return tmp;
}
Filtered Build Output:
g++.exe -Wzero-as-null-pointer-constant -std=c++14 -m64 -Wcast-align -Wundef -Wfloat-equal -Winline -Wunreachable-code -Wswitch-enum -Wswitch-default -Weffc++ -Wzero-as-null-pointer-constant -pg -g -D_WIN32 -D_UNICODE -Iinclude -c test.cpp -o obj\Debug\test.o
test.cpp: In member function 'Test* Test::onClone() const':
test.cpp:23:9: error: no match for 'operator<<' (operand types are 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}' and 'const Test')
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/istream:39:0,
from test.cpp:2:
%LibaryPath%/ostream:628:5: note: candidate: template<class _CharT, class _Traits, class _Tp> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&)
operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
^
%LibaryPath%/ostream:628:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/istream:39:0,
from test.cpp:2:
%LibaryPath%/ostream:574:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)
operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
^
%LibaryPath%/ostream:574:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/istream:39:0,
from test.cpp:2:
%LibaryPath%/ostream:569:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)
operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
^
%LibaryPath%/ostream:569:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/istream:39:0,
from test.cpp:2:
%LibaryPath%/ostream:556:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)
operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
^
%LibaryPath%/ostream:556:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/ostream:638:0,
from %LibaryPath%/istream:39,
from test.cpp:2:
%LibaryPath%/bits/ostream.tcc:321:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)
operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
^
%LibaryPath%/bits/ostream.tcc:321:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/istream:39:0,
from test.cpp:2:
%LibaryPath%/ostream:539:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)
operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
^
%LibaryPath%/ostream:539:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/istream:39:0,
from test.cpp:2:
%LibaryPath%/ostream:519:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)
operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
^
%LibaryPath%/ostream:519:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/istream:39:0,
from test.cpp:2:
%LibaryPath%/ostream:514:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)
operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
^
%LibaryPath%/ostream:514:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/istream:39:0,
from test.cpp:2:
%LibaryPath%/ostream:508:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)
operator<<(basic_ostream<char, _Traits>& __out, char __c)
^
%LibaryPath%/ostream:508:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/istream:39:0,
from test.cpp:2:
%LibaryPath%/ostream:502:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)
operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
^
%LibaryPath%/ostream:502:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/istream:39:0,
from test.cpp:2:
%LibaryPath%/ostream:497:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)
operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
^
%LibaryPath%/ostream:497:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/bits/ios_base.h:46:0,
from %LibaryPath%/ios:42,
from %LibaryPath%/istream:38,
from test.cpp:2:
%LibaryPath%/system_error:209:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::error_code&)
operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
^
%LibaryPath%/system_error:209:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
In file included from %LibaryPath%/string:52:0,
from %LibaryPath%/bits/locale_classes.h:40,
from %LibaryPath%/bits/ios_base.h:41,
from %LibaryPath%/ios:42,
from %LibaryPath%/istream:38,
from test.cpp:2:
%LibaryPath%/bits/basic_string.h:5167:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
%LibaryPath%/bits/basic_string.h:5167:5: note: template argument deduction/substitution failed:
test.cpp:23:18: note: mismatched types 'std::basic_ostream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf << (*this); //!< ERROR HERE
^
test.cpp:9:26: note: candidate: std::ostream& operator<<(std::ostream&, const Test&)
friend std::ostream& operator << (std::ostream& lhs, const Test& rhs) { rhs.onSave(lhs); return lhs; }
^
test.cpp:9:26: note: no known conversion for argument 1 from 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}' to 'std::ostream& {aka std::basic_ostream<char>&}'
test.cpp:24:9: error: no match for 'operator>>' (operand types are 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}' and 'Test')
buf >> (*tmp);
^
In file included from test.cpp:2:0:
%LibaryPath%/istream:924:5: note: candidate: template<class _CharT, class _Traits, class _Tp> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&&, _Tp&)
operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp& __x)
^
%LibaryPath%/istream:924:5: note: template argument deduction/substitution failed:
test.cpp:24:17: note: mismatched types 'std::basic_istream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf >> (*tmp);
^
In file included from test.cpp:2:0:
%LibaryPath%/istream:808:5: note: candidate: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*)
operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
^
%LibaryPath%/istream:808:5: note: template argument deduction/substitution failed:
test.cpp:24:17: note: mismatched types 'std::basic_istream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf >> (*tmp);
^
In file included from test.cpp:2:0:
%LibaryPath%/istream:803:5: note: candidate: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*)
operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s)
^
%LibaryPath%/istream:803:5: note: template argument deduction/substitution failed:
test.cpp:24:17: note: mismatched types 'std::basic_istream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf >> (*tmp);
^
In file included from test.cpp:2:0:
%LibaryPath%/istream:761:5: note: candidate: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char&)
operator>>(basic_istream<char, _Traits>& __in, signed char& __c)
^
%LibaryPath%/istream:761:5: note: template argument deduction/substitution failed:
test.cpp:24:17: note: mismatched types 'std::basic_istream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf >> (*tmp);
^
In file included from test.cpp:2:0:
%LibaryPath%/istream:756:5: note: candidate: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&)
operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c)
^
%LibaryPath%/istream:756:5: note: template argument deduction/substitution failed:
test.cpp:24:17: note: mismatched types 'std::basic_istream<char, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf >> (*tmp);
^
In file included from %LibaryPath%/istream:934:0,
from test.cpp:2:
%LibaryPath%/bits/istream.tcc:923:5: note: candidate: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&)
operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
^
%LibaryPath%/bits/istream.tcc:923:5: note: template argument deduction/substitution failed:
test.cpp:24:17: note: mismatched types 'std::basic_istream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf >> (*tmp);
^
In file included from %LibaryPath%/istream:934:0,
from test.cpp:2:
%LibaryPath%/bits/istream.tcc:955:5: note: candidate: template<class _CharT2, class _Traits2> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT2*)
operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
^
%LibaryPath%/bits/istream.tcc:955:5: note: template argument deduction/substitution failed:
test.cpp:24:17: note: mismatched types 'std::basic_istream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf >> (*tmp);
^
In file included from %LibaryPath%/string:53:0,
from %LibaryPath%/bits/locale_classes.h:40,
from %LibaryPath%/bits/ios_base.h:41,
from %LibaryPath%/ios:42,
from %LibaryPath%/istream:38,
from test.cpp:2:
%LibaryPath%/bits/basic_string.tcc:1441:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
operator>>(basic_istream<_CharT, _Traits>& __in,
^
%LibaryPath%/bits/basic_string.tcc:1441:5: note: template argument deduction/substitution failed:
test.cpp:24:17: note: mismatched types 'std::basic_istream<_CharT, _Traits>' and 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}'
buf >> (*tmp);
^
test.cpp:8:26: note: candidate: std::istream& operator>>(std::istream&, Test&)
friend std::istream& operator >> (std::istream& lhs, Test& rhs) { rhs.onLoad(lhs); return lhs; }
^
test.cpp:8:26: note: no known conversion for argument 1 from 'std::stringstream&() {aka std::__cxx11::basic_stringstream<char>&()}' to 'std::istream& {aka std::basic_istream<char>&}'
std::stringstream& buf();
buf << (*this); //!< ERROR HERE
Here buf is declared as a function returning a reference to a stringstream. If that was your intention, and you have defined that function elsewhere, you need to call the function to get the reference:
buf() << *this;
If you intended to have buf as a local stream, the declaration would be just
std::stringstream buf;
without the & and ().
The buf is a function declaration having no argument and return type as std::stringstream&. That's why no operator match error is shown by g++.
When creating a local object with default constructor, you should create it like,
std::stringstream buf;
It will remove no operator match errors.