التعبير الأساسي المتوقع في استدعاء الخوارزمية::binary_search

StackOverflow https://stackoverflow.com/questions/9438585

سؤال

آمل أن هذا ليس واضحا بشكل مؤلم.أتلقى هذا الخطأ الخفي:

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

والسطر الذي تشير إليه هو:

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

وصلت إلى هذا الخطأ بعد استخدام المكالمة الأبسط:

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.

رسالة الخطأ الثانية هي أنك حددت النوع عند تمرير الدالة كوسيطة.هذا هو في الأساس نفس استدعاء وظيفة كـ f(int x) بدلاً من f(x), ، وهو غير صحيح من الناحية النحوية.ينبغي أن يكون مجرد customArray::operator<.ومع ذلك، كما قلت من قبل، لن يساعدك ذلك لأنك ستتلقى رسالة الخطأ الأولى مرة أخرى.

في الأساس، لا يمكنك إجراء بحث ثنائي في قائمة مرتبطة.

نصائح أخرى

أنت تضع توقيع كامل من Functor الخاص بك داخل مكالمتك إلى Binary_Search.لا تحتاج إلى "Bool" هناك.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top