2012-02-24 7 views
0

これは痛いほど明白ではないことを願っています。アルゴリズム:: binary_searchコールで期待されるプライマリ式の呼び出し

fold.cpp:92: error: expected primary-expression before ‘)’ token 

それが参照している行は次のとおりです:

if (binary_search (corpus.begin(),corpus.end(), left)) 

となって:

if (binary_search (corpus.begin(),corpus.end(), left, customArray::operator<(customArray))) 

は、私はシンプルな呼び出しを使用した後、このエラーに到着した私は、この不可解なエラーを取得していますこのエラーメッセージ(重要な部分は最後の注記で、上記の呼び出しに変更すると言っています)

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))) 
{ 

} 

これはわかりやすいものです。私が明確にできる方法があれば教えてください。

答えて

0

あなたの最初のエラーメッセージが表示されました。このエラーは、比較関数を引数としてbinary_searchに渡すかどうかとは関係ありません。

2番目のエラーメッセージは、関数を引数として渡すときに型を指定したためです。これは基本的には、f(x)の代わりにf(int x)という関数を呼び出すのと同じですが、これは構文的には間違いです。それはちょうどcustomArray::operator<である必要があります。しかし、私が前に言ったように、あなたが最初のエラーメッセージをもう一度受け取るので、それはあなたを助けません。

基本的に、リンクリストでバイナリ検索を実行することはできません。

+0

ああ、これは私が実現したよりも大きな問題です。私はベクトルの使用に切り替える必要がありますね。どうもありがとうございました!私はこれを受け入れるとすぐにこの答を受け入れます。 –

0

バイナリ検索の呼び出しの中に、ファンクタの署名全体を入れています。そこに「ブール」は必要ありません。 binary_searchがイテレータに<を使用していますが、リストのイテレータは<をサポートしていないので、

+0

ありがとうございました!しかし、 'bool'が削除されても、それでもエラーが発生します:fold.cpp:92:error: ')'トークンの前に予期した一次式 –

+0

@MichaelRauh CustomArrayに演算子<それを明示的に渡す必要があります。 – Sid

+0

それは意味があります、私はベクトルバージョンを使用する際にそれを念頭に置く必要があります。 –

関連する問題