C++ 类堆栈的模板专门化

C++ 类堆栈的模板专门化,c++,templates,C++,Templates,我想写两个不同的类堆栈实现 (一) 模板 类堆栈{//。。。 }; (二) 模板 类堆栈{ }; 如果我在一个文件中定义这两个实现,我会得到编译器错误。是否可以将它们都放在同一个文件中 //compiler errors: Stack.hpp:119:46: error: declaration of template ‘template<class element_type, long unsigned int container_size> int containers::

我想写两个不同的类堆栈实现

(一)

模板
类堆栈{//。。。
};
(二)

模板
类堆栈{
};
如果我在一个文件中定义这两个实现,我会得到编译器错误。是否可以将它们都放在同一个文件中

//compiler errors:

Stack.hpp:119:46: error: declaration of template ‘template<class element_type, long unsigned int container_size> int containers::Stack()’
Stack.hpp:25:9: error: conflicts with previous declaration ‘template<class element_type, class container_type> class containers::Stack’
Stack.hpp:25:9: error: previous non-function declaration ‘template<class element_type, class container_type> class containers::Stack’
Stack.hpp:119:46: error: conflicts with function declaration ‘template<class element_type, long unsigned int container_size> int containers::Stack()’
//编译器错误:
Stack.hpp:119:46:错误:声明模板“template int containers::Stack()”
Stack.hpp:25:9:错误:与以前的声明“template class containers::Stack”冲突
hpp:25:9:错误:以前的非函数声明“模板类容器::堆栈”
Stack.hpp:119:46:错误:与函数声明“template int containers::Stack()冲突”

如果他们使用不同的参数,你必须给他们两个不同的名字。

如果他们使用不同的参数,你必须给他们两个不同的名字。

不,你不能。一个模板化类不能有两个具有不同模板参数的版本。我建议以不同的方式调用这两种
堆栈
,或者将它们放在不同的名称空间中。

不,你不能。一个模板化类不能有两个具有不同模板参数的版本。我建议以不同的方式调用这两种
堆栈
,或者将它们放在不同的名称空间中。

您必须编写如下内容:

template<typename element_type, typename container_type = std::vector<element_type> >
class Stack {
  // This is *not* the specialization
};

template<typename element_type>
class Stack<element_type, std::vector<element_type> > {
  // Specialization goes here.
};
模板
类堆栈{
//这不是专业化
};
模板
类堆栈{
//专业化就在这里。
};

编辑:您不能更改模板参数的类型。

您必须编写如下内容:

template<typename element_type, typename container_type = std::vector<element_type> >
class Stack {
  // This is *not* the specialization
};

template<typename element_type>
class Stack<element_type, std::vector<element_type> > {
  // Specialization goes here.
};
模板
类堆栈{
//这不是专业化
};
模板
类堆栈{
//专业化就在这里。
};

编辑:您不能更改模板参数的类型。

请告诉我,什么是编译器错误?什么是错误?什么编译器?请提供更详细的信息。请问,什么是编译器错误?什么错误?什么编译器?请提供更详细的信息。
template<typename element_type, typename container_type = std::vector<element_type> >
class Stack {
  // This is *not* the specialization
};

template<typename element_type>
class Stack<element_type, std::vector<element_type> > {
  // Specialization goes here.
};