0
votes

I have a template class where I am trying to define a member function outside class definition as follows:

class traits {
  typedef std::vector<int> container_t;
  ...other typedefs// 

};

template <class traits>
class Foo {
  typedef typename traits::container_t  container_t  
  // where container_t = std::vector <int>

  // member function to be templatized over container type
  void Initialize (container_t&);

private:
  container_t  temp;   //temp is of type std::vector<int>

};


template <typename T> 
void Foo <traits>::Initialize (T& data)  
{ 
   fill the data 
}

I want the function Initialize to take template container type -- container_t where container_t could be std::vector or std::set and so on.

But I get compiler error as

" prototype for Initialize (T& ) does not match any in the class Foo " " candidate is Initialize (container_t&) " ...

2
Do you mean for your traits class to be a template? As currently written, the question makes no sense. Why does Foo need to be a class template at all?Praetorian
@Praetorian: Except for the error in the definition of Initialize, it does make (at least some sense). Regardless of the template parameter being spelled exactly the same way as the type that is later used as argument, the Foo template can accept any type, not only traits.David Rodríguez - dribeas
@David Agreed, but the intent seems to be only to have traits as the template argument because the OP expects it have the container_t nested type. But then again, maybe he has multiple traits type classes defined.Praetorian
@Praetorian The traits class contains lot of typedefs (essentially it is a class with typedefs and static members.. There are a number of classes such as Foo which depend on traits class. It contains typedefs of container types so that I could change from vector to set and so on (ofcourse some implementation would change for filling the vector or set (push_back or insert) but that is ok for nowPogo

2 Answers

1
votes

Does this solve your problem?

template <class traits>
void Foo<traits>::Initialize( typename traits::container_t& t ) {
  // code
}
1
votes
template <typename T> 
//        vvvvvv   ^^                    mismatch!!!
void Foo <traits>::Initialize (T& data)  
{ 
   fill the data 
}

The template argument and the argument passed to the class template are not the same. You need to fix that. Also, I would recommend that you don't name the template argument with the same name and spelling as the type that will later be used in that place as that will often cause confusion. You could just change the capitalization:

class traits { … };
template <typename Traits>
class Foo { … };