Pogo
Pogo

Reputation: 495

writing templated member function over container type outside a template class

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&) " ...

Upvotes: 0

Views: 208

Answers (2)

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 { … };

Upvotes: 1

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275385

Does this solve your problem?

template <class traits>
void Foo<traits>::Initialize( typename traits::container_t& t ) {
  // code
}

Upvotes: 1

Related Questions