【发布时间】:2014-03-25 09:32:05
【问题描述】:
我是 C++ 新手,我正在学习使用模板。 我想制作一个带有 2 个模板参数的模板类,并专门化该类中的单个成员函数,以防第二个模板参数是模板化在第一个参数的指针类型上的向量。我认为我尝试过的一个例子会更清楚:
//Container.h:
template<class T , class CONT >
class Container {
private:
CONT container;
T someData;
public:
void foo();
};
我试过的 std::vector 的特化是:
//Container.cpp
template<class T>
void Container<T, std::vector<T*> > ::foo(){
cout << "specialized foo: " << container.pop_back();
}
template<class T, class CONT >
void Container<T, CONT > ::foo(){
cout << "regular foo: " << container.pop_back());
}
但我得到了这些错误:
error C3860: template argument list following class template name must list parameters in the order used in template parameter list
error C2976: 'Container<T,CONT>' : too few template argument
Container 类的用法必须是第一个参数是某种类型,第二个参数是 STL 容器、向量或列表。例如:
Container<int, vector<int*> > c;
c.foo();
我哪里错了?
【问题讨论】:
-
除了语法错误,不能部分特化函数模板,所以这种方法行不通。