我创建了一个继承 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'
似乎迭代器类型未在编译时定义。有什么解决方案可以解决这个问题吗?
原文由 S.Frank Richarrd 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以通过以下方式修复它:
或者