expected primary expression in algorithm::binary_search call
-
12-11-2019 - |
Question
I hope this is not painfully obvious. I am getting this cryptic error :
fold.cpp:92: error: expected primary-expression before ‘)’ token
The line that it is referring to is:
if (binary_search (corpus.begin(),corpus.end(), left, customArray::operator<(customArray)))
I arrived at this error after using the simpler call:
if (binary_search (corpus.begin(),corpus.end(), left))
and getting this error message (the important part is the note at the end, saying to change it to the above call)
In function ‘bool std::binary_search(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = std::_List_iterator<customArray>, _Tp = std::string [3]]’:
fold.cpp:92: instantiated from here
/usr/include/c++/4.2.1/bits/stl_algo.h:4240: error: no match for ‘operator<’ in ‘__val < __i. std::_List_iterator<_Tp>::operator* [with _Tp = customArray]()’
/usr/include/c++/4.2.1/bits/stl_algo.h: In function ‘_ForwardIterator std::lower_bound(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = std::_List_iterator<customArray>, _Tp = std::string [3]]’:
/usr/include/c++/4.2.1/bits/stl_algo.h:4239: instantiated from ‘bool std::binary_search(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = std::_List_iterator<customArray>, _Tp = std::string [3]]’
fold.cpp:92: instantiated from here
/usr/include/c++/4.2.1/bits/stl_algo.h:2906: error: no match for ‘operator<’ in ‘__middle. std::_List_iterator<_Tp>::operator* [with _Tp = customArray]() < __val’
fold.cpp:16: note: candidates are: bool customArray::operator<(customArray)
Essentially, I am trying to use a binary search on a linked list of custom (array type) objects. The rest of the relavent code is here:
// here is the custom class I am using in the list
class customArray
{
public:
// this is a somewhat lame way to compare, but it seems to work
bool operator< (customArray temp)
{
return array[0] < temp.array[0];
}
bool operator> (customArray temp)
{
return array[0] > temp.array[0];
}
bool operator== (customArray temp)
{
return ((array[0] == temp.array[0]) && (array[1] == temp.array[1]) && (array[2] == temp.array[2]));
}
string array[3];
};
//All of this stuff is in main
customArray one;
//some processing here to fill one
corpus.push_back (one);
// sort the list
corpus.sort();
corpus.unique();
string left [3];
if (binary_search (corpus.begin(),corpus.end(), left, customArray::operator<(customArray)))
{
}
I hope this is easy to understand. Let me know if there is any way that I can clarify.
Solution
Your first error message was because binary_search
uses <
on the iterators, but list's iterators don't support <
. This error has nothing to do with whether or not you pass a comparison function as an argument to binary_search
.
Your second error message is because you specified the type when passing the function as an argument. That's basically the same as calling a function as f(int x)
instead of f(x)
, which is syntactically incorrect. It should just be customArray::operator<
. However, as I said before, that won't help you because you'll just get the first error message again.
Basically you can't perform a binary search on a linked list.
OTHER TIPS
You are putting the whole signature of your functor inside your call to binary_search. You don't need the 'bool' there.