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,
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 am attempting to write a simple max pairwise product function in C++ to get my feet wet with this language.
The intent is to pass a vector to the maxPairwise function, sort the entire vector, then multiply together the last two integers. The function should return an integer. In my IDE (eclipse 2020-03 (4.15.0)) in the Main function, it shows that “ vector vect{8,3,3,7}” has an error, and when I try to compile, it says there are 20 errors, shown below. What is the problem?
My Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int maxPairwise(const vector<int> &x){
sort(x.begin(),x.end());
int a = x.end()[-2];
int b = x.end()[-3];
return a * b;
int main() {
vector<int> vect {8,3,3,7};
int ans = maxPairwise(vect);
cout<<ans<<endl;
return 0;
}
The Error:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:632:1: note: candidate template ignored: could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'const int'
swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/type_traits:4590:1: note: candidate template ignored: requirement 'is_move_assignable<const int>::value' was not satisfied [with _Tp = const int]
swap(_Tp& __x, _Tp& __y) _NOEXCEPT_(is_nothrow_move_constructible<_Tp>::value &&
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:270:1: note: candidate template ignored: could not match '_Tp [_Np]' against 'const int'
swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:632:1: note: candidate template ignored: could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'const int'
swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/tuple:163:6: note: candidate template ignored: could not match '__tuple_leaf<_Ip, type-parameter-0-1, >' against 'const int'
void swap(__tuple_leaf<_Ip, _Hp, _Ep>& __x, __tuple_leaf<_Ip, _Hp, _Ep>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/tuple:944:1: note: candidate template ignored: could not match 'tuple<type-parameter-0-0...>' against 'const int'
swap(tuple<_Tp...>& __t, tuple<_Tp...>& __u)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2310:6: note: candidate template ignored: could not match '__compressed_pair<type-parameter-0-0, type-parameter-0-1>' against 'const int'
void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2958:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const int'
swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4945:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const int'
swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:5244:1: note: candidate template ignored: could not match 'weak_ptr<type-parameter-0-0>' against 'const int'
swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/functional:2472:1: note: candidate template ignored: could not match 'function<type-parameter-0-0 (type-parameter-0-1...)>' against 'const int'
swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
make: *** [src/Play2.o] Error 1
"make all" terminated with exit code 2. Build might be incomplete.
06:49:26 Build Failed. 20 errors, 0 warnings. (took 926ms)
'''
I have a question concerning accumulate() and operator overloading.
I have a class Order that contains
private:
Customer cust;
std::vector<Purchase> vP;
and a class Purchase with
private:
string name;
double unit_price;
int count;
I want to use accumulate() to add together the prices of all of the Purchases (which means unit_price*count) in a vector with Orders. My solution with overloading the + operator of Order doesn't work. This is how it looks like:
double Order::operator+(Order& p) {
double gesamt{};
gesamt=(gesamtpreisEinerPerson()+p.gesamtpreisEinerPerson());
return gesamt;
}
double Order::gesamtpreisEinerPerson(){
double kosten=0.0;
for (int i=0; i<vP.size(); ++i){
kosten+=(vP.at(i).getPrice()*vP.at(i).getCount());
}
return kosten;
}
and for accumulate I call
double totalPrice(vector<Order> vectOrd) {
double d = accumulate(vectOrd.begin(), vectOrd.end(), 0.0);
return d;
}
So my question is: Why can't I solve the accumulate problem with operator overloading? Doesn't it work in general or is there something wrong with my code? Thanks very much in advance!
The error message is
In file included from main.cpp:5:
In file included from ./global.hpp:16:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/numeric:81:25: error: invalid operands to binary expression ('double' and 'Order')
__init = __init + *__first;
~~~~~~ ^ ~~~~~~~~
./global.hpp:272:16: note: in instantiation of function template specialization 'std::__1::accumulate<std::__1::__wrap_iter<Order *>, double>' requested here
double d = accumulate(vectOrd.begin(), vectOrd.end(), 0.0);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iterator:743:1: note: candidate template ignored: could not match 'reverse_iterator<type-parameter-0-0>' against 'Order'
operator+(typename reverse_iterator<_Iter>::difference_type __n, const reverse_iterator<_Iter>& __x)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iterator:1163:1: note: candidate template ignored: could not match 'move_iterator<type-parameter-0-0>' against 'Order'
operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iterator:1576:1: note: candidate template ignored: could not match '__wrap_iter<type-parameter-0-0>' against 'Order'
operator+(typename __wrap_iter<_Iter>::difference_type __n,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3698:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'double'
operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3711:1: note: candidate template ignored: could not match 'const _CharT *' against 'double'
operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3723:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'Order'
operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3734:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'double'
operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3746:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'double'
operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3760:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'double'
operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3768:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'double'
operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3776:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'double'
operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3784:1: note: candidate template ignored: could not match 'const _CharT *' against 'double'
operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3792:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'Order'
operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3801:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'double'
operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3809:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'double'
operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
^
1 error generated.
make[2]: *** [build/Debug/GNU-MacOSX/main.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
The accumulator predicate should have signature double(double, const Order&):
Easy way to do your accumulation would be:
double d = accumulate(vectOrd.begin(),
vectOrd.end(),
0.0,
[](double acc, const Order& order) {
return acc + order.gesamtpreisEinerPerson();
});
If you really want to use operator overloading, you have to implement (the free function):
double operator+ (double d, const Order& order)
{
return d + order.gesamtpreisEinerPerson();
}
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.
I am new to C++ and was messing around with some of the things that I've learnt.
So I tried the following code:
#include <iostream>
int main() {
std::cout << std::cin;
}
So I expected the code to return an error, but instead got what I think is a memory address (0x6fccc408). Also, when running it multiple times, I got the same memory address, even after restarting cmd. What exactly does this memory address signify?
It looks like you are using C++98 or C++03. In those versions cin could be implicitly converted to a void* so what you are seeing is that conversion.
If you used C++11, which got rid of the implicit void* conversion and instead implemented a explicit conversion to bool this would produce a compiler error like
clang++ -std=c++11 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
main.cpp:4:15: error: invalid operands to binary expression ('ostream' (aka 'basic_ostream<char>') and 'istream' (aka 'basic_istream<char>'))
std::cout << std::cin;
~~~~~~~~~ ^ ~~~~~~~~
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:245:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'const void *' for 1st argument; take the address of the argument with &
operator<<(const void* __p)
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/system_error:209:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>] not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'const std::error_code' for 2nd argument
operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:108:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to '__ostream_type &(*)(__ostream_type &)' (aka 'basic_ostream<char, std::char_traits<char> > &(*)(basic_ostream<char, std::char_traits<char> > &)') for 1st argument
operator<<(__ostream_type& (*__pf)(__ostream_type&))
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:117:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to '__ios_type &(*)(__ios_type &)' (aka 'basic_ios<char, std::char_traits<char> > &(*)(basic_ios<char, std::char_traits<char> > &)') for 1st argument
operator<<(__ios_type& (*__pf)(__ios_type&))
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:127:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'std::ios_base &(*)(std::ios_base &)' for 1st argument
operator<<(ios_base& (*__pf) (ios_base&))
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:166:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'long' for 1st argument
operator<<(long __n)
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:170:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'unsigned long' for 1st argument
operator<<(unsigned long __n)
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:174:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'bool' for 1st argument
operator<<(bool __n)
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:178:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'short' for 1st argument
operator<<(short __n);
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:181:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'unsigned short' for 1st argument
operator<<(unsigned short __n)
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:189:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'int' for 1st argument
operator<<(int __n);
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:192:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'unsigned int' for 1st argument
operator<<(unsigned int __n)
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:201:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'long long' for 1st argument
operator<<(long long __n)
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:205:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'unsigned long long' for 1st argument
operator<<(unsigned long long __n)
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:220:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'double' for 1st argument
operator<<(double __f)
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:224:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'float' for 1st argument
operator<<(float __f)
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:232:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'long double' for 1st argument
operator<<(long double __f)
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:270:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to '__streambuf_type *' (aka 'basic_streambuf<char, std::char_traits<char> > *') for 1st argument
operator<<(__streambuf_type* __sb);
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:502:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>] not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'char' for 2nd argument
operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:508:5: note: candidate function [with _Traits = std::char_traits<char>] not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'char' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __out, char __c)
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:514:5: note: candidate function [with _Traits = std::char_traits<char>] not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'signed char' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:519:5: note: candidate function [with _Traits = std::char_traits<char>] not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'unsigned char' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:556:5: note: candidate function [with _Traits = std::char_traits<char>] not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'const char *' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:569:5: note: candidate function [with _Traits = std::char_traits<char>] not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'const signed char *' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:574:5: note: candidate function [with _Traits = std::char_traits<char>] not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'const unsigned char *' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:628:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>, _Tp = std::basic_istream<char>] not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'basic_ostream<char, std::char_traits<char> > &&' for 1st argument
operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/bits/ostream.tcc:321:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>] not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'const char *' for 2nd argument
operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:497:5: note: candidate template ignored: deduced conflicting types for parameter '_CharT' ('char' vs. 'std::basic_istream<char>')
operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/bits/basic_string.h:5325:5: note: candidate template ignored: could not match 'basic_string' against 'basic_istream'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:539:5: note: candidate template ignored: could not match 'const _CharT *' against 'istream' (aka 'basic_istream<char>')
operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
^
1 error generated.