在模板类之外的容器类型上编写模板化的成员函数
writing templated member function over container type outside a template class
我有一个模板类,我试图在类定义之外定义一个成员函数,如下所示:
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等等。
但是我得到编译错误
" prototype for Initialize (T&)不匹配类Foo中的任何"候选是Initialize (container_t&)"…
这解决你的问题了吗?
template <class traits>
void Foo<traits>::Initialize( typename traits::container_t& t ) {
// code
}
template <typename T>
// vvvvvv ^^ mismatch!!!
void Foo <traits>::Initialize (T& data)
{
fill the data
}
模板实参和传递给类模板的实参不相同。你需要解决这个问题。另外,我建议不要将模板参数命名为与稍后在该位置使用的类型相同的名称和拼写,因为这通常会引起混淆。你可以改变大小写:
class traits { … };
template <typename Traits>
class Foo { … };
相关文章:
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 将公共但非静态的成员函数与ALGLIB集成
- 使用指向成员的指针将成员函数作为参数传递
- 将重载的成员函数传递给函数模板
- 我不小心调用了一个没有自己类对象的成员函数.但这是怎么回事呢
- 如何在C++中使用非静态成员函数作为回调函数
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 关联容器的下界复杂性:成员函数与非成员函数
- 在 C++ 中用派生类型重写成员函数
- 链表的泛型函数remove()与成员函数remove)
- 如何将lambda作为模板类的成员函数参数
- constexpr构造函数需要常量成员函数时出现问题
- 将自由函数绑定为类成员函数
- 区分非成员函数和头文件中的成员函数
- 如何从子成员函数修改父公共成员变量
- 保留对其他类的成员函数的引用
- 在运算符重载定义中使用成员函数(const错误)
- 内联如何影响模块接口中的成员函数
- 将成员函数指针作为参数传递给模板方法