Related
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 implemented operator overloading as follows (see the methods bool operator== and bool operator<):
#include "../data_types.h"
class OffsetValuePair {
private:
unsigned_value offset;
unsigned_value value;
public:
OffsetValuePair(unsigned_value address, unsigned_value value) {
this->offset = address;
this->value = value;
}
bool operator==(const OffsetValuePair offsetValuePair) {
return this->getOffset() == offsetValuePair.offset;
}
bool operator<(const OffsetValuePair offset_value_pair) {
return this->getValue() < offset_value_pair.value;
}
unsigned_value getOffset() {
return offset;
}
unsigned_value getValue() {
return value;
}
};
I'm using the operator for finding the lower bound using will use my overloaded operator for less than (<):
const auto lower_bound_offset_value_pair = OffsetValuePair(0, 1234);
const auto lower_bound = std::lower_bound(pointer_map_sorted_by_value_.begin(),
pointer_map_sorted_by_value_.end(),
lower_bound_offset_value_pair);
When I compile the code, I get the following error with AppleClang:
/Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm:719:71: error: invalid operands to binary expression ('const OffsetValuePair' and 'const OffsetValuePair')
bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
~~~ ^ ~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm:4285:13: note: in instantiation of member function 'std::__1::__less<OffsetValuePair, OffsetValuePair>::operator()' requested here
if (__comp(*__m, __value_))
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm:4307:12: note: in instantiation of function template specialization 'std::__1::__lower_bound<std::__1::__less<OffsetValuePair, OffsetValuePair> &, std::__1::__wrap_iter<OffsetValuePair *>, OffsetValuePair>' requested here
return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm:4316:19: note: in instantiation of function template specialization 'std::__1::lower_bound<std::__1::__wrap_iter<OffsetValuePair *>, OffsetValuePair, std::__1::__less<OffsetValuePair, OffsetValuePair> >' requested here
return _VSTD::lower_bound(__first, __last, __value_,
^
/Users/bully/Desktop/PointerSearcher/src/pointer_search_objects/PointerSearcher.h:660:33: note: in instantiation of function template specialization 'std::__1::lower_bound<std::__1::__wrap_iter<OffsetValuePair *>, OffsetValuePair>' requested here
const auto lower_bound = std::lower_bound(pointer_map_sorted_by_value_.begin(),
^
/Users/bully/Desktop/PointerSearcher/src/pointer_search_objects/OffsetValuePair.h:21:7: note: candidate function not viable: 'this' argument has type 'const OffsetValuePair', but method is not marked const
bool operator<(const OffsetValuePair offset_value_pair) {
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/utility:572:1: note: candidate template ignored: could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'const OffsetValuePair'
operator< (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/iterator:702:1: note: candidate template ignored: could not match 'reverse_iterator<type-parameter-0-0>' against 'const OffsetValuePair'
operator<(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/iterator:1143:1: note: candidate template ignored: could not match 'move_iterator<type-parameter-0-0>' against 'const OffsetValuePair'
operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/iterator:1512:1: note: candidate template ignored: could not match '__wrap_iter<type-parameter-0-0>' against 'const OffsetValuePair'
operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT_DEBUG
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/tuple:1187:1: note: candidate template ignored: could not match 'tuple<type-parameter-0-0...>' against 'const OffsetValuePair'
operator<(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:2920:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const OffsetValuePair'
operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:2978:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const OffsetValuePair'
operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:2987:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const OffsetValuePair'
operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:4758:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const OffsetValuePair'
operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:4823:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const OffsetValuePair'
operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:4831:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const OffsetValuePair'
operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
Using gcc and MSVC the code compiles fine, I'm only getting this error on Mac OS X with the standard platform compiler (AppleClang).
I do not understand why AppleClang does not accept the code. What is wrong with it?
I cannot declare the methods const since they use a this referenced method call. If I convert the this object into a 2nd method argument I get another error telling me that the method signature is wrong: error: 'bool OffsetValuePair::operator<(OffsetValuePair, OffsetValuePair)' must have exactly one argument
Nevermind, I just found the solution:
bool operator==(const OffsetValuePair offsetValuePair) const {
return this->offset == offsetValuePair.offset;
}
bool operator<(const OffsetValuePair offset_value_pair) const {
return this->value < offset_value_pair.value;
}
The changes were to define both methods as const as suggested by the error message. Furthermore, when using the this reference method calls are not allowed due to the const method definition so I replaced the getter with the direct member access.
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();
}
Hi I have 2 vectors of objects and I want to compare if they are equal (means v[i] == m[i]). I have overload operator== for my class but I still have a comp lire errors.
struct Node {
Node() {};
Node(int st, int end) : l(st), r(end) {
if (st < 0 || end < st) {
throw "Bad input";
}
}
Node(int st, int end, int m, int s) : l(st), r(end), sum(s), min(m) {
if (st < 0 || end < st) {
throw "Bad input";
}
}
bool operator== (Node& node) {
if (node.l == l && node.r == r && node.sum == sum && node.min == min)
return true;
return false;
}
int l = 0, r = 0;
int sum = 0;
int min = 0;
};
int main() {
vector<Node> segTree {} , exampleTree {};
exampleTree.push_back(Node(0,0,1,1));
if ( segTree == exampleTree )
throw "smith";
return 0;
}
Error:
In file included from test.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream:38:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:216:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__locale:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:439:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:658:97: error: invalid operands to binary expression
('const Node' and 'const Node')
_LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
~~~ ^ ~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:1164:14: note: in instantiation of member function
'std::__1::__equal_to<Node, Node>::operator()' requested here
if (!__pred(*__first1, *__first2))
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:1176:19: note: in instantiation of function template
specialization 'std::__1::equal<std::__1::__wrap_iter<const Node *>, std::__1::__wrap_iter<const Node *>, std::__1::__equal_to<Node, Node> >' requested here
return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:3192:41: note: in instantiation of function template
specialization 'std::__1::equal<std::__1::__wrap_iter<const Node *>, std::__1::__wrap_iter<const Node *> >' requested here
return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
^
test.cpp:83:18: note: in instantiation of function template specialization 'std::__1::operator==<Node, std::__1::allocator<Node> >' requested here
if ( segTree == exampleTree )
^
test.cpp:66:6: note: candidate function not viable: 'this' argument has type 'const Node', but method is not marked const
bool operator== (Node& node) {
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:403:1: note: candidate template ignored: could not match
'pair<type-parameter-0-0, type-parameter-0-1>' against 'const Node'
operator==(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:574:1: note: candidate template ignored: could not match
'reverse_iterator<type-parameter-0-0>' against 'const Node'
operator==(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:866:6: note: candidate template ignored: could not match
'istreambuf_iterator<type-parameter-0-0, type-parameter-0-1>' against 'const Node'
bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:969:1: note: candidate template ignored: could not match
'move_iterator<type-parameter-0-0>' against 'const Node'
operator==(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:1285:1: note: candidate template ignored: could not match
'__wrap_iter<type-parameter-0-0>' against 'const Node'
operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/tuple:877:1: note: candidate template ignored: could not match
'tuple<type-parameter-0-0...>' against 'const Node'
operator==(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1791:6: note: candidate template ignored: could not match
'allocator<type-parameter-0-0>' against 'const Node'
bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2876:1: note: candidate template ignored: could not match
'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const Node'
operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2912:1: note: candidate template ignored: could not match
'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const Node'
operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2920:1: note: candidate template ignored: could not match
'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const Node'
operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4716:1: note: candidate template ignored: could not match
'shared_ptr<type-parameter-0-0>' against 'const Node'
operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4765:1: note: candidate template ignored: could not match
'shared_ptr<type-parameter-0-0>' against 'const Node'
operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4773:1: note: candidate template ignored: could not match
'shared_ptr<type-parameter-0-0>' against 'const Node'
operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
^
1 error generated.
Your operator== should take a const Node&, and be const itself. You're not modifying either node!
I have two classes TempFileand Year. TempFileis getting a .txt file with temperatures of the last 130 years. The constructor reads this file and store the data in a std::vector<Year>. Now I want to add a sort function, that shows me the average temperature of every year in descending order. But when I make a structto define my compare (third parameter in std::sort) I am getting too many errors. I´ve also tried to overload the >-operator. Didn't work, too.
class TempFile {
public:
TempFile(std::string temp_file); // read from .txt file and push_back() the data into std::vector<Year> _years.
//The vector and constructor are working fine.
int get_number_years() const;
std::vector<Year> get_vector() const;
void sort_descending() const;
private:
std::vector<Year> _years;
struct descending
{
bool operator() (const Year& y1, const Year& y2)
{
return (y1.get_average_temp() > y2.get_average_temp());
}
} temp_descending;
};
int TempFile::get_number_years() const
{
return _years.size();
}
std::vector<Year> TempFile::get_vector() const
{
return _years;
}
void TempFile::sort_descending() const
{
std::sort(_years.begin(), _years.end(), temp_descending);
}
class Year {
public:
Year(int year, float january, float february, ...);
void print() const;
float get_added_months() const;
float get_average_temp() const;
int get_year() const;
private:
int _year;
float _january, _february, ...; // all months
};
float Year::get_added_months() const
{
return (_januar + _februar + _maerz + _april + _mai + _juni + _juli + _august + _september + _oktober
+ _november + _dezember);
}
float Year::get_average_temp() const
{
return (get_added_months() / 12);
}
int Year::get_year() const
{
return _jahreszahl;
}
Here is a snippet of the error messages:
g++ -c -g -MMD -MP -MF "build/Debug/GNU-MacOSX/TempFile.o.d" -o build/Debug/GNU-MacOSX/TempFile.o TempFile.cpp
In file included from TempFile.cpp:8:
In file included from ./TempFile.h:10:
In file included from ./Year.h:10:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:265:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:625:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/type_traits:3534:9: error: no viable overloaded '='
__x = _VSTD::move(__y);
~~~ ^ ~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3852:17: note: in instantiation of function template specialization 'std::__1::swap<const Year>' requested here
swap(*__first, *__last);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:4037:5: note: in instantiation of function template specialization 'std::__1::__sort<TempFile::absteigend &, const Year *>' requested here
__sort<_Comp_ref>(__first, __last, __comp);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:4071:12: note: in instantiation of function template specialization 'std::__1::sort<const Year *, TempFile::absteigend &>' requested here
_VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
^
TempFile.cpp:102:10: note: in instantiation of function template specialization 'std::__1::sort<const Year, TempFile::absteigend>' requested here
std::sort(_years.begin(), _years.end(), temp_absteigend);
^
./Year.h:13:7: note: candidate function (the implicit copy assignment operator) not viable: 'this' argument has type 'const Year', but method is not marked const
class Year {
^
In file included from TempFile.cpp:8:
In file included from ./TempFile.h:10:
In file included from ./Year.h:10:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:218:1: note: candidate template ignored: could not match '_Tp [_Np]' against 'const Year'
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:456:1: note: candidate template ignored: could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year'
swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2385:1: note: candidate template ignored: could not match '__compressed_pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year'
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:2874:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const Year'
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:4861:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const Year'
swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:5150:1: note: candidate template ignored: could not match 'weak_ptr<type-parameter-0-0>' against 'const Year'
swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
^
In file included from TempFile.cpp:8:
In file included from ./TempFile.h:10:
In file included from ./Year.h:10:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:265:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:15:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3621:5: error: no matching function for call to 'swap'
swap(*__x, *__y); // x > y && y <= z
^~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/type_traits:3530:1: note: candidate template ignored: substitution failure [with _Tp = const Year]
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:218:1: note: candidate template ignored: could not match '_Tp [_Np]' against 'const Year'
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:456:1: note: candidate template ignored: could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year'
swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2385:1: note: candidate template ignored: could not match '__compressed_pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year'
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:2874:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const Year'
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/algorithm:4071:12: note: in instantiation of function template specialization 'std::__1::sort<const Year *, TempFile::absteigend &>' requested here
_VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
^
TempFile.cpp:102:10: note: in instantiation of function template specialization 'std::__1::sort<const Year, TempFile::absteigend>' requested here
std::sort(_years.begin(), _years.end(), temp_absteigend);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3598:1: note: candidate template ignored: substitution failure [with _Compare = TempFile::absteigend &, _ForwardIterator = const Year *]
__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3641:9: error: no matching function for call to 'swap'
swap(*__x3, *__x4);
^~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/type_traits:3530:1: note: candidate template ignored: substitution failure [with _Tp = const Year]
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:218:1: note: candidate template ignored: could not match '_Tp [_Np]' against 'const Year'
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:456:1: note: candidate template ignored: could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year'
swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2385:1: note: candidate template ignored: could not match '__compressed_pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year'
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:2874:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const Year'
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:4861:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const Year'
swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:5150:1: note: candidate template ignored: could not match 'weak_ptr<type-parameter-0-0>' against 'const Year'
swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
^
In file included from TempFile.cpp:8:
In file included from ./TempFile.h:10:
In file included from ./Year.h:10:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:265:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:15:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3645:13: error: no matching function for call to 'swap'
swap(*__x2, *__x3);
^~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/type_traits:3530:1: note: candidate template ignored: substitution failure [with _Tp = const Year]
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:218:1: note: candidate template ignored: could not match '_Tp [_Np]' against 'const Year'
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/memory:2385:1: note: candidate template ignored: could not match '__compressed_pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year'
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:2874:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const Year'
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:4861:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const Year'
swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:5150:1: note: candidate template ignored: could not match 'weak_ptr<type-parameter-0-0>' against 'const Year'
swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
^
In file included from TempFile.cpp:8:
In file included from ./TempFile.h:10:
In file included from ./Year.h:10:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:265:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:15:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3671:13: error: no matching function for call to 'swap'
swap(*__x3, *__x4);
^~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/type_traits:3530:1: note: candidate template ignored: substitution failure [with _Tp = const Year]
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:218:1: note: candidate template ignored: could not match '_Tp [_Np]' against 'const Year'
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:456:1: note: candidate template ignored: could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year'
swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2385:1: note: candidate template ignored: could not match '__compressed_pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year'
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:2874:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const Year'
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:4861:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const Year'
swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:5150:1: note: candidate template ignored: could not match 'weak_ptr<type-parameter-0-0>' against 'const Year'
swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
^
In file included from TempFile.cpp:8:
In file included from ./TempFile.h:10:
In file included from ./Year.h:10:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:265:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:15:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3675:17: error: no matching function for call to 'swap'
swap(*__x2, *__x3);
^~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/type_traits:3530:1: note: candidate template ignored: substitution failure [with _Tp = const Year]
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:218:1: note: candidate template ignored: could not match '_Tp [_Np]' against 'const Year'
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:456:1: note: candidate template ignored: could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year'
swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2385:1: note: candidate template ignored: could not match '__compressed_pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year'
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:2874:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const Year'
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:4861:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const Year'
swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:5150:1: note: candidate template ignored: could not match 'weak_ptr<type-parameter-0-0>' against 'const Year'
swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
^
In file included from TempFile.cpp:8:
In file included from ./TempFile.h:10:
In file included from ./Year.h:10:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:265:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:15:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3679:21: error: no matching function for call to 'swap'
swap(*__x1, *__x2);
^~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/type_traits:3530:1: note: candidate template ignored: substitution failure [with _Tp = const Year]
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:218:1: note: candidate template ignored: could not match '_Tp [_Np]' against 'const Year'
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:456:1: note: candidate template ignored: could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year'
swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2385:1: note: candidate template ignored: could not match '__compressed_pair<type-parameter-0-0, type-parameter-0-1>' against 'const Year'
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:2874:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const Year'
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:4861:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const Year'
swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:5150:1: note: candidate template ignored: could not match 'weak_ptr<type-parameter-0-0>' against 'const Year'
swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
^
In file included from TempFile.cpp:8:
In file included from ./TempFile.h:10:
In file included from ./Year.h:10:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:265:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:15:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3729:5: error: no matching function for call to '__sort3'
__sort3<_Compare>(__first, __first+1, __j, __comp);
^~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3866:20: note: in instantiation of function template specialization 'std::__1::__insertion_sort_3<TempFile::absteigend &, const Year *>' requested here
_VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:4037:5: note: in instantiation of function template specialization 'std::__1::__sort<TempFile::absteigend &, const Year *>' requested here
__sort<_Comp_ref>(__first, __last, __comp);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:4071:12: note: in instantiation of function template specialization 'std::__1::sort<const Year *, TempFile::absteigend &>' requested here
_VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
^
TempFile.cpp:102:10: note: in instantiation of function template specialization 'std::__1::sort<const Year, TempFile::absteigend>' requested here
std::sort(_years.begin(), _years.end(), temp_absteigend);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3598:1: note: candidate template ignored: substitution failure [with _Compare = TempFile::absteigend &, _ForwardIterator = const Year *]
__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3739:22: error: no viable overloaded '='
*__j = _VSTD::move(*__k);
~~~~ ^ ~~~~~~~~~~~~~~~~~
./Year.h:13:7: note: candidate function (the implicit copy assignment operator) not viable: 'this' argument has type 'const Year', but method is not marked const
class Year {
^
In file included from TempFile.cpp:8:
In file included from ./TempFile.h:10:
In file included from ./Year.h:10:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:265:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:15:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:3742:18: error: no viable overloaded '='
*__j = _VSTD::move(__t);
~~~~ ^ ~~~~~~~~~~~~~~~~
./Year.h:13:7: note: candidate function (the implicit copy assignment operator) not viable: 'this' argument has type 'const Year', but method is not marked const
class Year {
^
TempFile::sort_descending() is marked const, while you're trying to modify the _years member via std::sort.
Removing the const specifier from the function's signature or making _years a mutable will solve this problem.