简体   繁体   中英

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? 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". 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:

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

and then FwdIter can be deduced.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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