Joy*_*Joy 1 c++ templates dictionary stl class
我写了这个小代码
std::map<int,template<class T>> map_;
map_.insert(make_pair<int,message>(myMsg.id,myMsg));
Run Code Online (Sandbox Code Playgroud)
但编译器似乎没有得到它并显示为错误
template argument 2 is invalid
Run Code Online (Sandbox Code Playgroud)
当我试图通过这样做来纠正时
template<class T>
std::map<int,T> map_;
Run Code Online (Sandbox Code Playgroud)
它显示为错误:
'template' 之前预期的主要表达式 |
错误:预期为“;” 在“模板”之前
我对此不确定,但您正在尝试声明一个变量,并且该定义必须完全定义。尝试使用模板并不能完全定义变量。
如果您愿意,可以将其包装在结构中:
template<typename T>
struct Wrapper
{
typedef std::map<int, T> map_type;
};
Run Code Online (Sandbox Code Playgroud)
然后像这样使用它:
Wrapper<std::string>::map_type my_wrapped_map;
my_wrapped_map[1] = "Foo";
Run Code Online (Sandbox Code Playgroud)