I am struggling to understand why the following code does not allow an implicit conversion to occur.
#include <string>
using namespace std;
struct HasConversionToString {
HasConversionToString(const string& s_) : s{s_} {}
string s;
operator const string&() const { return s; }
};
int main() {
string s{"a"};
HasConversionToString obj{"b"};
return s < obj;
}
Both clang and gcc fail to find a valid way to compare the two objects with errors along the lines of:
clang++ -std=c++14 -Wall -Wextra -pedantic conversion.cpp -o test
conversion.cpp:13:12: error: invalid operands to binary expression ('string' (aka 'basic_string<char>') and 'HasConversionToString')
return s < obj;
~ ^ ~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.3.0/../../../../include/c++/5.3.0/bits/stl_pair.h:220:5: note: candidate template ignored: could not match
'pair' against 'basic_string'
operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.3.0/../../../../include/c++/5.3.0/bits/stl_iterator.h:298:5: note: candidate template ignored: could not match
'reverse_iterator' against 'basic_string'
operator<(const reverse_iterator<_Iterator>& __x,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.3.0/../../../../include/c++/5.3.0/bits/stl_iterator.h:348:5: note: candidate template ignored: could not match
'reverse_iterator' against 'basic_string'
operator<(const reverse_iterator<_IteratorL>& __x,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.3.0/../../../../include/c++/5.3.0/bits/stl_iterator.h:849:5: note: candidate template ignored: could not match
'__normal_iterator' against 'basic_string'
operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.3.0/../../../../include/c++/5.3.0/bits/stl_iterator.h:856:5: note: candidate template ignored: could not match
'__normal_iterator' against 'basic_string'
operator<(const __normal_iterator<_Iterator, _Container>& __lhs,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.3.0/../../../../include/c++/5.3.0/bits/stl_iterator.h:1089:5: note: candidate template ignored: could not match
'move_iterator' against 'basic_string'
operator<(const move_iterator<_IteratorL>& __x,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.3.0/../../../../include/c++/5.3.0/bits/stl_iterator.h:1095:5: note: candidate template ignored: could not match
'move_iterator' against 'basic_string'
operator<(const move_iterator<_Iterator>& __x,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.3.0/../../../../include/c++/5.3.0/bits/basic_string.h:4989:5: note: candidate template ignored: could not match
'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'HasConversionToString'
operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.3.0/../../../../include/c++/5.3.0/bits/basic_string.h:5001:5: note: candidate template ignored: could not match
'const _CharT *' against 'HasConversionToString'
operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.3.0/../../../../include/c++/5.3.0/bits/basic_string.h:5013:5: note: candidate template ignored: could not match
'const _CharT *' against 'string' (aka 'basic_string<char>')
operator<(const _CharT* __lhs,
^
1 error generated.
whereas the following code works fine, when I explicitly cast the object to a string.
#include <string>
using namespace std;
struct HasConversionToString {
HasConversionToString(const string& s_) : s{s_} {}
string s;
operator const string&() const { return s; }
};
int main() {
string s{"a"};
HasConversionToString obj{"b"};
return s < static_cast<string>(obj);
}
based on the rules and examples listed on cppreference for implicit casts, I see no reason this shouldn't work. I assume that both clang and gcc didn't screw up the same thing, so I imagine that I've got a conceptual misunderstanding.
The one you want to call is a function template:
template<class charT, class Traits, class Alloc>
bool operator<(std::basic_string<charT, Traits, Alloc> const& lhs,
std::basic_string<charT, Traits, Alloc> const& rhs);
Deduction fails for the second argument because a HasConversionToString is not a std::basic_string - template argument deduction doesn't look through implicit conversions. As a result, that function template is removed from overload resolution.
std::experimental::basic_string_view has a similar problem, which was solved by a "sufficient additional overloads" rule (the library must add enough overloads so that comparison between a basic_string_view and something convertible to one works).
You don't really want such a thing for basic_string, though - having < possibly silently causing a trip to the heap is not really a good idea.
http://en.cppreference.com/w/cpp/language/template_argument_deduction#Implicit_conversions
Type deduction does not consider implicit conversions (other than type
adjustments listed above): that's the job for overload resolution,
which happens later.
Related
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 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();
}
Code:
template <typename T>
void merge_sort(std::vector<T>& vector)
{
if (vector.size() < 2)
return;
std::vector<T> left, right;
for (int i = 0; i < vector.size(); ++i)
{
if (i % 2 == 0)
left.push_back(vector[i]);
else
right.push_back(vector[i]);
}
merge_sort(left);
merge_sort(right);
sort(vector,left, right);
}
template<typename T>
void sort(std::vector<T>& v, std::vector<T>& left, std::vector<T>& right)
{
int k = 0;
while ((left.size() != 0) && (right.size() != 0))
if (left[0] <= right[0])
{
v[k++] = left[0];
left.erase(v.begin());
}
else
{
v[k++] = right[0];
right.erase(v.begin());
}
while (left.size() != 0)
{
v[k++] = left[0];
left.erase(v.begin());
}
while (right.size() !=0)
{
v[k++] = right[0];
right.erase(v.begin());
}
}
Here is the full error message:
clang++ -stdlib=libstdc++ -std=c++1y -Wall -pedantic -g -O3 src/main.cpp -o project1.out
In file included from src/main.cpp:11:
In file included from src/reporting.hpp:10:
In file included from /usr/bin/../lib/gcc/i686-linux-gnu/5.3.1/../../../../include/c++/5.3.1/algorithm:62:
/usr/bin/../lib/gcc/i686-linux-gnu/5.3.1/../../../../include/c++/5.3.1/bits/stl_algo.h:1964:22: error: invalid operands to
binary expression ('std::vector<int, std::allocator<int> >' and 'std::vector<int, std::allocator<int> >')
std::__lg(__last - __first) * 2,
~~~~~~ ^ ~~~~~~~
/usr/bin/../lib/gcc/i686-linux-gnu/5.3.1/../../../../include/c++/5.3.1/bits/stl_algo.h:4729:12: note: in instantiation of
function template specialization 'std::__sort<std::vector<int, std::allocator<int> >,
__gnu_cxx::__ops::_Iter_comp_iter<std::vector<int, std::allocator<int> > > >' requested here
std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
^
src/sorting.hpp:83:2: note: in instantiation of function template specialization 'std::sort<std::vector<int,
std::allocator<int> >, std::vector<int, std::allocator<int> > >' requested here
sort(vector,left, right);
^
src/main.cpp:21:64: note: in instantiation of function template specialization 'merge_sort<int>' requested here
std::vector<sorter_t<int>> sorters = {insertion_sort<int>, merge_sort<int>, hybrid_sort<int>};
^
/usr/bin/../lib/gcc/i686-linux-gnu/5.3.1/../../../../include/c++/5.3.1/bits/stl_bvector.h:208:3: note: candidate function
not viable: no known conversion from 'std::vector<int, std::allocator<int> >' to 'const std::_Bit_iterator_base' for
1st argument
operator-(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
^
/usr/bin/../lib/gcc/i686-linux-gnu/5.3.1/../../../../include/c++/5.3.1/bits/stl_iterator.h:328:5: note: candidate template
ignored: could not match 'reverse_iterator' against 'vector'
operator-(const reverse_iterator<_Iterator>& __x,
^
/usr/bin/../lib/gcc/i686-linux-gnu/5.3.1/../../../../include/c++/5.3.1/bits/stl_iterator.h:380:5: note: candidate template
ignored: could not match 'reverse_iterator' against 'vector'
operator-(const reverse_iterator<_IteratorL>& __x,
^
/usr/bin/../lib/gcc/i686-linux-gnu/5.3.1/../../../../include/c++/5.3.1/bits/stl_iterator.h:911:5: note: candidate template
ignored: could not match '__normal_iterator' against 'vector'
operator-(const __normal_iterator<_IteratorL, _Container>& __lhs,
^
/usr/bin/../lib/gcc/i686-linux-gnu/5.3.1/../../../../include/c++/5.3.1/bits/stl_iterator.h:923:5: note: candidate template
ignored: could not match '__normal_iterator' against 'vector'
operator-(const __normal_iterator<_Iterator, _Container>& __lhs,
^
/usr/bin/../lib/gcc/i686-linux-gnu/5.3.1/../../../../include/c++/5.3.1/bits/stl_iterator.h:1138:5: note: candidate template
ignored: could not match 'move_iterator' against 'vector'
operator-(const move_iterator<_IteratorL>& __x,
^
/usr/bin/../lib/gcc/i686-linux-gnu/5.3.1/../../../../include/c++/5.3.1/bits/stl_iterator.h:1145:5: note: candidate template
ignored: could not match 'move_iterator' against 'vector'
operator-(const move_iterator<_Iterator>& __x,
^
In file included from src/main.cpp:11:
In file included from src/reporting.hpp:10:
In file included from /usr/bin/../lib/gcc/i686-linux-gnu/5.3.1/../../../../include/c++/5.3.1/algorithm:62:
/usr/bin/../lib/gcc/i686-linux-gnu/5.3.1/../../../../include/c++/5.3.1/bits/stl_algo.h:1878:18: error: invalid operands to
binary expression ('std::vector<int, std::allocator<int> >' and 'std::vector<int, std::allocator<int> >')
if (__last - __first > int(_S_threshold))
~~~~~~ ^ ~~~~~~~
/usr/bin/../lib/gcc/i686-linux-gnu/5.3.1/../../../../include/c++/5.3.1/bits/stl_algo.h:1966:9: note: in instantiation of
function template specialization 'std::__final_insertion_sort<std::vector<int, std::allocator<int> >,
__gnu_cxx::__ops::_Iter_comp_iter<std::vector<int, std::allocator<int> > > >' requested here
std::__final_insertion_sort(__first, __last, __comp);
^
/usr/bin/../lib/gcc/i686-linux-gnu/5.3.1/../../../../include/c++/5.3.1/bits/stl_algo.h:4729:12: note: in instantiation of
function template specialization 'std::__sort<std::vector<int, std::allocator<int> >,
__gnu_cxx::__ops::_Iter_comp_iter<std::vector<int, std::allocator<int> > > >' requested here
std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
^
src/sorting.hpp:83:2: note: in instantiation of function template specialization 'std::sort<std::vector<int,
std::allocator<int> >, std::vector<int, std::allocator<int> > >' requested here
sort(vector,left, right);
^
src/main.cpp:21:64: note: in instantiation of function template specialization 'merge_sort<int>' requested here
std::vector<sorter_t<int>> sorters = {insertion_sort<int>, merge_sort<int>, hybrid_sort<int>};
^
/usr/bin/../lib/gcc/i686-linux-gnu/5.3.1/../../../../include/c++/5.3.1/bits/stl_bvector.h:208:3: note: candidate function
not viable: no known conversion from 'std::vector<int, std::allocator<int> >' to 'const std::_Bit_iterator_base' for
1st argument
operator-(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
^
/usr/bin/../lib/gcc/i686-linux-gnu/5.3.1/../../../../include/c++/5.3.1/bits/stl_iterator.h:328:5: note: candidate template
ignored: could not match 'reverse_iterator' against 'vector'
operator-(const reverse_iterator<_Iterator>& __x,
^
/usr/bin/../lib/gcc/i686-linux-gnu/5.3.1/../../../../include/c++/5.3.1/bits/stl_iterator.h:380:5: note: candidate template
ignored: could not match 'reverse_iterator' against 'vector'
operator-(const reverse_iterator<_IteratorL>& __x,
^
/usr/bin/../lib/gcc/i686-linux-gnu/5.3.1/../../../../include/c++/5.3.1/bits/stl_iterator.h:911:5: note: candidate template
ignored: could not match '__normal_iterator' against 'vector'
operator-(const __normal_iterator<_IteratorL, _Container>& __lhs,
^
/usr/bin/../lib/gcc/i686-linux-gnu/5.3.1/../../../../include/c++/5.3.1/bits/stl_iterator.h:923:5: note: candidate template
ignored: could not match '__normal_iterator' against 'vector'
operator-(const __normal_iterator<_Iterator, _Container>& __lhs,
^
/usr/bin/../lib/gcc/i686-linux-gnu/5.3.1/../../../../include/c++/5.3.1/bits/stl_iterator.h:1138:5: note: candidate template
ignored: could not match 'move_iterator' against 'vector'
operator-(const move_iterator<_IteratorL>& __x,
^
/usr/bin/../lib/gcc/i686-linux-gnu/5.3.1/../../../../include/c++/5.3.1/bits/stl_iterator.h:1145:5: note: candidate template
ignored: could not match 'move_iterator' against 'vector'
operator-(const move_iterator<_Iterator>& __x,
^
2 errors generated.
Your code breaks because you are using erase incorrecrly: you need to pass the begin() of the vector being erased.
However, using erase is a bad idea to begin with, because it makes your sort asymptotically slower by a factor of N. You should make a pair of iterators, one for left and the other one for right, and move them as you progress through your arrays. This will keep merge an O(n) operation, instead of O (n^2) in your current implementation.
left.erase(v.begin());
Never ever do this. foo.erase requires a member of foo.
Even if you wrote left.erase(left.begin());, this would still be a terrible coding, and I mean terrible. Unless you erase last element, or one very close to the end, erasing a vector element is slow like hell. It takes O(N) steps. So don't erase (or insert, for that matter) vector elements, unless you are absolutely sure it is a right thing to do.
The whole point of the merge sort is to do sorting in O(N log N) steps. By using erase, you make it O(N**2*log N) steps.
And don't ever call your function "sort", because there is a sort function already in STL. It is confusing. Calling a global (not member) function "sort", "exp", "abs", etc. is really a bad idea. With member functions, it is slightly better. Calling your function "sort" is as if I would change my name to Matt Mclaugin, and went to live near you.
Newbies also like to call a vector "list". I'll never understand why.
This is the some of the error message....
error: invalid operands to binary expression ('std::vector >' and 'std::vector >') std::__lg(__last - __first) * 2,
You should copy the error message(s) exactly as they were, not "some of the error message".
Besides, your code is not complete. You should present enough of your code so that we'll be able to reproduce your error.
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!