我正在创建一个模板化的快速排序函数,它应该允许我使用迭代器快速排序一个定制的双向链表类。我收到错误
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
据我了解,在研究了这个问题并阅读了其他人收到的解决方案之后,我在模板声明之外直接弄乱了我的列表类。根据编译器,我认为当我的主要快速排序函数调用递归函数时会发生这种情况?我不完全确定是我做了什么导致了这个问题。
现在,这是我第一次尝试这样的模板函数,通过模板调用另一个类。如果这实际上有很多错误,我不会感到惊讶,而它只是向我展示了一个。如果有人还可以告诉我我在此尝试中犯的任何其他错误,请也包括这些错误。