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

发布于 2024-09-12 11:36:16 字数 400 浏览 2 评论 0原文

下面的代码说 错误:预期为“;”在“放弃”之前 错误:预期为“;”在“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你另情深 2024-09-19 11:36:16

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

在你的情况下

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

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;
回首观望 2024-09-19 11:36:16

添加typename

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

由于map依赖于模板参数,所以直到模板实例化后才知道iterator是否为类型或静态成员;除非您使用 typename 指定它是一种类型,否则编译器将假定后者。

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.

难以启齿的温柔 2024-09-19 11:36:16

您必须通过 typename 关键字告诉编译器 map::iterator 是一种类型。

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

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

typename map<T,int>::iterator forwit;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文