![](/img/trans.png)
[英]C++: Trying to declare and define templated class within templated class. What is wrong with my code?
[英]how to declare templated map::iterator within a templated class. following code Says ; expected when compiled
以下代码表示错误:expected';' 在'forwit'错误之前:预期';' 在'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.
我完全不知道问题是什么? 请帮忙。
提前致谢
为了帮助编译器理解您正在讨论模板化上下文中的类型,您必须帮助它编写typename
。
在你的情况下
typename map<T,int>::iterator forwit;
添加typename
:
typename map<T,int>::iterator forwit;
typename map<int,T>::iterator revit;
由于map<T,int>
取决于模板参数,因此在模板实例化之前不知道iterator
是类型还是静态成员; 除非使用typename
指定它是一个类型,否则编译器将假定后者。
你必须通过typename
关键字告诉编译器map<T,int>::iterator
是一个类型。
typename map<T,int>::iterator forwit;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.