如何在模板化类中声明模板化 map::iterator。下面的代码说;编译时预期
下面的代码说 错误:预期为“;”在“放弃”之前 错误:预期为“;”在“revit”之前
template<class T>
class mapping {
public:
map<T,int> forw;
map<int,T> rev;
int curr;
//typeof(forw)::iterator temp;
map<T,int>::iterator forwit;
map<int,T>::iterator revit;
};
// }; // JVC: This was present, but unmatched.
我完全不知道问题是什么?请帮忙。
提前致谢
the following code says
error: expected ‘;’ before ‘forwit’
error: expected ‘;’ before ‘revit’
template<class T>
class mapping {
public:
map<T,int> forw;
map<int,T> rev;
int curr;
//typeof(forw)::iterator temp;
map<T,int>::iterator forwit;
map<int,T>::iterator revit;
};
// }; // JVC: This was present, but unmatched.
i have completely no idea what the problem is? please help.
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了帮助编译器理解您正在谈论模板化上下文中的类型,您必须帮助它编写
typename
。在你的情况下
To help the compiler understand you are talking about a type in a templated context, you have to help it writing
typename
.In your case
添加
typename
:由于
map
依赖于模板参数,所以直到模板实例化后才知道iterator
是否为类型或静态成员;除非您使用typename
指定它是一种类型,否则编译器将假定后者。Add
typename
:Since
map<T,int>
depends on the template parameter, it isn't known until the template is instantiated whetheriterator
is a type or a static member; unless you usetypename
to specify that it is a type, the compiler will assume the latter.您必须通过
typename
关键字告诉编译器map::iterator
是一种类型。You have to tell the compiler
map<T,int>::iterator
is a type by thetypename
keyword.