繁体   English   中英

C ++:模板快速排序函数中收到“无法推断出模板参数”错误

[英]C++: Receiving “couldn't deduce template parameter” error in template quicksort function

我正在创建一个模板化的快速排序函数,该函数应该允许我使用迭代器对自定义的双向链接列表类进行快速排序。 我遇到了错误

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

根据我的理解,在研究了这个问题并通读了其他人提供的解决方案之后,我莫名其妙地直接在模板声明之外弄乱了我的列表类。 根据编译器,我认为当我的主要quicksort函数调用递归函数时会发生这种情况吗? 我不完全确定引起此问题的原因是什么。

现在,这是我第一次尝试使用此类模板功能,并通过模板调用另一个类。 如果实际上有很多错误,我不会感到惊讶,尽管它只是向我展示了一个错误。 如果任何人也可以将我在此尝试中遇到的任何其他错误告知我,请也将其包括在内。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM