开发者

how to declare templated map::iterator within a templated class. following code Says ; expected when compiled

the following code says 开发者_如何学JAVA 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


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<T,int>::iterator forwit;


Add typename:

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

Since map<T,int> depends on the template parameter, it isn't known until the template is instantiated whether iterator is a type or a static member; unless you use typename to specify that it is a type, the compiler will assume the latter.


You have to tell the compiler map<T,int>::iterator is a type by the typename keyword.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜