如何在模板化的类中声明模板化的map :: iterator。下面的代码说;编译时预计

时间:2010-07-29 13:43:46

标签: c++ templates stl map compiler-errors

以下代码说明  错误:在'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.

我完全不知道问题是什么?请帮忙。

提前致谢

3 个答案:

答案 0 :(得分:9)

为了帮助编译器理解您正在讨论模板化上下文中的类型,您必须帮助它编写typename

在你的情况下

typename map<T,int>::iterator forwit;

答案 1 :(得分:3)

添加typename

typename map<T,int>::iterator forwit;
typename map<int,T>::iterator revit;

由于map<T,int>取决于模板参数,因此在实例化模板之前不知道iterator是类型还是静态成员;除非你使用typename来指定它是一个类型,否则编译器将假设后者。

答案 2 :(得分:1)

您必须告诉编译器map<T,int>::iteratortypename关键字的类型。

typename map<T,int>::iterator forwit;