在模板类外部的容器类型上书写模板成员函数 [英] writing templated member function over container type outside a template class

查看:122
本文介绍了在模板类外部的容器类型上书写模板成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板类,我想在类定义之外定义一个成员函数,如下:

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 
}

我想要的函数Initialize取模板容器类型 - container_t其中container_t可以是std :: vector或std :: set等等。

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.

但是我收到编译器错误,因为初始化(T&)的原型与

But I get compiler error as

class Foo
candidate是Initialize(container_t&)...

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

推荐答案

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

这篇关于在模板类外部的容器类型上书写模板成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆