我写了这个小代码
std::map<int,template<class T>> map_;
map_.insert(make_pair<int,message>(myMsg.id,myMsg));
但编译器似乎没有得到它并显示为错误
template argument 2 is invalid
当我试图通过这样做纠正
template<class T>
std::map<int,T> map_;
显示为错误:
在'template'之前预期的primary-expression |
错误:预期';'在“模板”之前
答案 0 :(得分:3)
我不确定这一点,但是你试图声明一个变量,并且必须完全定义该定义。尝试使用模板并不能完全定义变量。
如果您愿意,可以将其包装在结构中:
template<typename T>
struct Wrapper
{
typedef std::map<int, T> map_type;
};
然后像这样使用它:
Wrapper<std::string>::map_type my_wrapped_map;
my_wrapped_map[1] = "Foo";
答案 1 :(得分:1)
您可能没有使用C ++ 11编译器,并且此行无效:
std::map<int,template<class T>> map_;
应该是
std::map<int,template<class T> > map_;
注意> >
之间的空格。在预C ++ 11中,>>
始终被视为位移运算符。
除此之外,代码应该是什么?如果我没弄错的话,你应该将你的地图声明为
std::map<int,message> map_;
现在,std::map<int,template<class T>> map_;
没有意义,除非这是另一个模板类的成员,在这种情况下你需要
std::map<int,T> map_;
答案 2 :(得分:-1)
define KEYINT_MAP(T) std::map<int, T>
KEYINT_MAP(class) map;