【发布时间】:2019-03-27 16:03:50
【问题描述】:
我创建了一个继承std::map 的类,并尝试使用一种方法在特定索引处获取值。
#define MYAPI_EXPORTS
#ifdef MYAPI_EXPORTS
#define MY_API __declspec(dllexport)
#else
#define MY_API __declspec(dllimport)
#endif
template<class _Value>
class MY_API MyDictionary : public std::map<std::string, _Value>
{
_Value GetItem(int index)
{
std::map<std::string, _Value>::iterator itr = this->begin(); //compile error at this line
int c = 0;
while (c < index)
{
itr++;
c++;
}
return itr->second;
}
};
'std::map::iterator itr' 这一行在编译时显示错误。
错误是
error C2760: syntax error: unexpected token 'identifier', expected ';'
error C7510: 'iterator': use of dependent type name must be prefixed with 'typename'
似乎迭代器类型未在编译时定义。有没有办法解决这个问题?
【问题讨论】:
-
use of dependent type name must be prefixed with 'typename'. -
您正在使用
map,就好像它是vector。如果index是关键,您可以使用地图operator[] -
你只需要把
typename放在std::map<std::string, _Value>::iterator itr之前 -
如何在 std::map 之前使用 typename<:string _value>::iterator itr ?
-
强制性注释:使用
_Value作为标识符是未定义的行为。所有以下划线和大写字母开头的标识符都保留用于实现(与任何以两个下划线开头的标识符一样)。
标签: c++