アルゴリズムでの予想されるプライマリ式:: binary_search呼び出し
-
12-11-2019 - |
質問
これが痛いほど明白ではないことを願っています。私はこの暗号エラーを得ています:
fold.cpp:92: error: expected primary-expression before ‘)’ token
.
参照している線:
if (binary_search (corpus.begin(),corpus.end(), left, customArray::operator<(customArray)))
.
Simple Callを使用した後、このエラーに到着しました:
if (binary_search (corpus.begin(),corpus.end(), left))
.
とこのエラーメッセージの入手(重要な部分は最後のメモで、上記の通話に変更することを言っています)
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)
.
本質的に、私はカスタム(配列タイプ)オブジェクトのリンクリストでバイナリ検索を使用しようとしています。Relaventコードの残りの部分はこちらです。
// 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)))
{
}
.
私はこれが理解しやすいことを願っています。明確にできる方法があるかどうか私に知らせてください。
解決
最初のエラーメッセージは、binary_search
がイテレータで<
を使用しているためでしたが、リストのイテレータは<
をサポートしません。このエラーは、比較関数をbinary_search
の引数として渡すかどうかには関係ありません。
あなたの2番目のエラーメッセージは、関数を引数として渡すときに型を指定したためです。それは基本的にf(int x)
の代わりにf(x)
として関数を呼び出すのと同じです。これは構文的に正しくありません。それはcustomArray::operator<
だけです。しかし、私が前に言ったように、それはあなたがちょうど最初のエラーメッセージをもう一度取得するのであなたを助けることはありません。
基本的には、リンクリストでバイナリ検索を実行できません。
他のヒント
あなたはあなたのファンクタの全体をbinary_searchにあなたの呼び出しの中であなたの呼び出しの中に置いています。そこに「ブール」を必要としません。
所属していません StackOverflow