【发布时间】:2018-01-12 07:16:54
【问题描述】:
我有一个这样的代码片段
map<string, map<string, map<string, float> > > map_f;
map<string, map<string, map<string, string> > > map_s;
map<string, map<string, map<string, double> > > map_d;
我只想把这段代码简明扼要地写成这样:
myMap<float> map_f;
myMap<string> map_s;
myMap<double> map_d;
所以我尝试使用template 和using 来实现:
template<type T>
using myMap = map<string, map<string, map<string, T> > >;
但是,我得到一个错误:
error: expected expression
template<type T>
我的问题是,如何修改这段代码,这个错误是什么意思?
代码已显示:
#include <string>
#include <map>
using namespace std;
int main()
{
template<typename T>
using myMap = map<string, map<string, map<string, T> > >;
return 0;
}
我用c++ 4.2.1 @ mac osX 10.13.2编译它:
g++ -std=c++11 temp_def_cls.cxx -o main
我得到了错误:
temp_def_cls.cxx:7:3: error: expected expression
template<typename T>
^
1 error generated.
【问题讨论】:
-
不应该是
template<typename T> using myMap = ...吗? -
@songyuanyao 还是不行,在你的电脑上可以吗?
-
@springcc Yes.
-
你不能在函数内部做。将其移出函数。
-
@songyuanyao 是的,解决了!