我有一个模板类,我试图在类定义之外定义一个成员函数,如下所示:
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 等等。
但是我得到了编译器错误
“初始化 (T&) 的原型(prototype)与 Foo 类中的任何原型(prototype)都不匹配” “候选人是 Initialize (container_t&)” ...
最佳答案
这是否解决了您的问题?
template <class traits>
void Foo<traits>::Initialize( typename traits::container_t& t ) {
// code
}
关于c++ - 在模板类之外的容器类型上编写模板化成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22495864/