C ++:模板快速排序函数

时间:2015-05-04 20:01:53

标签: c++ templates compiler-errors quicksort template-function

我正在创建一个模板化的quicksort函数,它允许我使用迭代器快速定制一个定制的双向链表类。我收到了错误

In file included from main.cpp:21.0:
quicksort.h: In instantiation of 'void quicksort(listclass&) [with
listclass = inkist<int>]':
main.cpp:156:23: required from here
quicksort.h:36:33: error: no matching function for call to 
'_quicksort(inkist<int>::iterator&, inkist<int>::iterator&)'
   _quicksort(headfish, tailfish);

quicksort.h:36:33: note: candidate is:
quicksort.h:40:13: note: template<class listclass> void _quicksort (class
listclass::iterator&, class listclass::iterator&)
  inline void _quicksort(class listclass::iterator& headfish, class
listclass::iterator& tailfish)

quicksort.h:40:13: note: template argument deduction/substitution failed.
quicksort.h:36:33: note: couldn't deduce template parameter 'listclass'
   _quicksort(headfish, tailfish);

这是我的代码:

#ifndef _quicksort_h_included_
#define _quicksort_h_included_


#include <iterator>

template <class listclass>
inline void swap(class listclass::iterator& onefish, class listclass::iterator& twofish)
{
   class listclass::const_iterator tempfish;
   *tempfish=*onefish
   *onefish=*twofish;
   *twofish=*tempfish;
};

template <class listclass>
inline void quicksort (listclass& redlist)
{
   class listclass::iterator headfish;
   headfish=redlist.begin();
   class listclass::iterator tailfish;
   tailfish=redlist.end();

   _quicksort(headfish, tailfish);
};

template <class listclass>
inline void _quicksort(class listclass::iterator& headfish, class listclass::iterator& tailfish)
{
   if (headfish.P!=tailfish.P && headfish.P!=tailfish.P->next)
   {
      class listclass::iterator itrfish = partition(headfish, tailfish);
      class listclass::iterator lowfish(itrfish.P->prev);
      class listclass::iterator highfish(itrfish.P->next);

      _quicksort(headfish, lowfish);
      _quicksort(highfish, tailfish);
   }
};

template <class listclass>
inline class listclass::iterator partition(class listclass::iterator& headfish, class listclass::iterator& tailfish)
{
   class listclass::iterator datafish;
   *datafish=*headfish;

   class listclass::iterator pvtfish(headfish.P->prev);
   class listclass::iterator sortfish;

   for (sortfish.P=headfish.P; sortfish.P!=tailfish.P; sortfish.P=sortfish.P->next)
   {
      if(*sortfish<=*datafish)
      {
         if (sortfish.P==0)
            sortfish.P=headfish.P;
         else
            sortfish.P++;

         swap(&sortfish, &pvtfish);
      }
   }
   if (sortfish.P==0)
      sortfish.P=headfish.P;
   else
      sortfish.P++;

   swap(&sortfish, &tailfish);

   return sortfish;
};



#endif

根据我的理解,在研究了这个问题并阅读了其他人收到的解决方案之后,我在某种程度上直接在模板声明之外弄乱了我的listclass。根据编译器,我认为它发生在我的主要快速排序函数调用递归函数时?我不完全确定我做了什么导致了这个问题。

现在,这是我第一次尝试使用这样的模板函数,通过模板调用另一个类。如果在这方面确实存在很多错误,我不会感到惊讶,而它只是向我展示了一个错误。如果有人也可以告诉我我在这次尝试中犯的任何其他错误,请同时包含这些错误。

1 个答案:

答案 0 :(得分:1)

template <class listclass>
inline void _quicksort(class listclass::iterator& headfish, class listclass::iterator& tailfish);

此处listclass用于“非推断上下文”。它无法通过任何方式从函数参数中推断出来。

因此,一种解决方案是指定它而不是试图让编译器推导出它:

template <class listclass>
inline void quicksort (listclass& redlist)
{
   // ...

   _quicksort<listclass>(headfish, tailfish);
};

或者,您可以像这样更改_quicksort

template <class FwdIter>
inline void _quicksort(FwdIter& headfish, FwdIter& tailfish);

然后FwdIter可以推断出来。

相关问题