【发布时间】:2011-06-25 11:13:39
【问题描述】:
我想构建一个Stack 类,以便用户能够选择他想使用哪个容器来实现Stack。例如,List/Vector。
部分代码:
stack.h
#ifndef STACK_H_
#define STACK_H_
template <typename T, template<typename T> class ContainerType>
class Stack{
ContainerType<T> container;
public:
Stack() : container(ContainerType<T>()){}
};
#endif /* STACK_H_ */
test.cpp
#include "stack.h"
#include <vector>
int main(){
Stack<int, std::vector<int> > stack;
return 0;
}
好吧,它不编译。我在线收到下一个错误:
Stack<int, std::vector<int> > stack;
错误:
expected a class template, got `std::vector<int, std::allocator<int> >' test.cpp
invalid type in declaration before ';' token test.cpp
type/value mismatch at argument 2 in template parameter
list for `template<class T, template<class T> class ContainerType>
class Stack' test.cpp
【问题讨论】:
-
这个错误是由于总是写
using namespace std;并习惯它的蹩脚做法的结果。 -
@Armen 但真正的错误在其他地方。