Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在模板化类中声明模板化映射::迭代器。以下代码表示:;编译时应为_C++_Templates_Stl_Map_Compiler Errors - Fatal编程技术网

C++ 如何在模板化类中声明模板化映射::迭代器。以下代码表示:;编译时应为

C++ 如何在模板化类中声明模板化映射::迭代器。以下代码表示:;编译时应为,c++,templates,stl,map,compiler-errors,C++,Templates,Stl,Map,Compiler Errors,下面的代码表示 错误:应为“;”在“福威特”之前 错误:应为“;”在“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

下面的代码表示 错误:应为“;”在“福威特”之前 错误:应为“;”在“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.
模板
类映射{
公众:
地图forw;
地图修订版;
国际货币;
//typeof(forw)::迭代器温度;
map::迭代器forwit;
地图::迭代器revit;
};
//    }; // JVC:这是存在的,但无与伦比。
我完全不知道问题出在哪里?请帮忙


提前感谢

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

就你而言

typename map<T,int>::iterator forwit;
typename映射::迭代器forwit;

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

typename map<T,int>::iterator forwit;
typename映射::迭代器forwit;

添加
typename

typename map<T,int>::iterator forwit;
typename map<int,T>::iterator revit;
typename映射::迭代器forwit;
typename地图::迭代器revit;

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

您能发布一些真实的代码吗?这不会编译,因为
模板
类映射
之间没有任何内容。此外,为了帮助您,我们需要知道
map
指的是什么。是typedef吗?用户定义的类?mukul,为什么要将代码格式编辑成更差的版本?之前我只使用
标记来格式化代码,发现只有一行显示为格式化。然后我阅读了帮助,找到了放置
块的一般惯例,它也很好地工作(显示了整个代码块的格式)。因此我编辑了代码。@mukul:如果这个答案解决了你的问题(看起来是这样的),你应该接受它。这就是StackOverflow的工作原理。我一直在等待计时器完成。它说的是再花7分钟接受一个答案,只是一个简单的问题。如果所有的答案都让你满意,并且解决了我的问题,那么如何接受这个答案呢。我应该接受最早的答复还是什么。这个用户提供了非常精确的答案。要奖励所有答案,只需投赞成票。公认的答案适用于寻找相同答案的人。你应该选择最精确的一个(例如有有趣的评论)。这是为什么?实际上,我不明白为什么你不需要在VisualC++上放置那个“类型名”。此外,当您使用模板化CLS时,有什么逻辑理由这样做?谢谢