简体   繁体   English

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

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

I'm creating a templated quicksort function which should allow me to quicksort a custom-made doubly linked list class using iterators. 我正在创建一个模板化的快速排序函数,该函数应该允许我使用迭代器对自定义的双向链接列表类进行快速排序。 I'm getting the error 我遇到了错误

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

Here's my code: 这是我的代码:

#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

From my understanding, after researching this problem and reading through the solutions other people have received, I'm somehow messing directly with my listclass outside of the template declaration. 根据我的理解,在研究了这个问题并通读了其他人提供的解决方案之后,我莫名其妙地直接在模板声明之外弄乱了我的列表类。 According to the compiler, I think it happens when my main quicksort function calls the recursive function? 根据编译器,我认为当我的主要quicksort函数调用递归函数时会发生这种情况吗? I'm not entirely sure what it is that I've done that has caused this issue. 我不完全确定引起此问题的原因是什么。

Now, this is sort of my first time experimenting with template functions like this, with calling another class through a template. 现在,这是我第一次尝试使用此类模板功能,并通过模板调用另一个类。 I wouldn't be surprised if there's actually a lot of mistakes in this, while it's only showing me the one. 如果实际上有很多错误,我不会感到惊讶,尽管它只是向我展示了一个错误。 If anybody can also inform me of any other mistakes I have made with this attempt, please include those as well. 如果任何人也可以将我在此尝试中遇到的任何其他错误告知我,请也将其包括在内。

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

Here listclass is used in a "non deduced context". 此处的listclass用于“非推论上下文”中。 It cannot be deduced from the function arguments by any means. 不能以任何方式从函数参数推论得出。

So one solution would be to specify it instead of trying to make the compiler deduce it: 因此,一种解决方案是指定它,而不是尝试使编译器推断它:

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

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

Or, you could change _quicksort like this: 或者,您可以像这样更改_quicksort

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

and then FwdIter can be deduced. 然后可以推导出FwdIter

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

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