0

Excuse me in advance for my bad english..i'm developing my own STL and i have some problem using template. This is the main structure of the my List class:

List.h

template <class T, class N>
class List{

   public:
          //public methods

   private:
          //private methods
}

Ok, now i need to implement a member function "sort" that will sort the elements of the list, but in this sort function i need to istantiate a List object. I found a solution implementing that function in a different module in this way:

AuxiliaryFunction.h

#include<List.h>
template <class List>
void sort(Lista & l){
   List A; //Works fine!
   ..
}

But in this way i have to call the sort function by doing this:

sort(ListObject);

instead of

ListObject.sort();

How can I do the same thing but with a member function of the List class? What would be different if the List class was an abstract class like the following code?

 template <class T, class N>
    class List{

       public:
              typedef T ElemType;
              typedef P position;

              virtual void create() = 0;
              virtual bool empty() = 0;
              ..
              void merge(Lista< T, N > &, Lista< T, N > &);
              ...
       private:
              //private methods
    }

Can be a template template parameter be a solution? I tried by doing something like this

 template <class T, class N>
    class List{
         ...
         template <template <class T,class N> class List> sort(){
              List A;
              ..
         }
    }

but a C2783 error ('could not deduce template argument for List') appear when i write

ListObject.sort();

Any kind of help will be appreciated :)

Andrea
  • 13
  • 4
  • http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – chris Dec 04 '13 at 02:13

1 Answers1

0

It sounds like you're looking for something like this:

template <class T, class N>
class List{
     ...
     void sort(){
          List<T, N> A;
          ..
     }
}
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285