d - 猜猜这是什么? ;)
#include<vector>
template<class Key, class CollT = std::vector>
CollT* delete_(Key kValue)
{
return new CollT;
}
int main()
{
return 0;
}
我收到了一个错误。 那是怎么回事?
答案 0 :(得分:3)
std :: vector不是一个类,它是一个模板。您可以使用语法将模板指示为模板参数。在您的情况下,您可能只想将其设为std::vector<Key>
答案 1 :(得分:0)
我认为您展示的代码已损坏: 这里是修复和如何构建,这适用于g ++ 4.4.5所以仔细检查你的版本:
#include<vector>
template <class Key, class CollT = std::vector<Key> >
CollT* delete_(Key kValue)
{
return new CollT;
}
int main()
{
return 0;
}
构建:
g ++ templdef.cpp -std = c ++ 0x
编辑基于评论:
1) replace typename with class within template definition (both class CollT and typename CollT seems fine)
2) replaced `vector<int>` with `vector<Key>`
3) compiling without flag, will give you the following error: default template arguments may not be used in function templates without -std=c++0x or -std=gnu++0x