gpt4 book ai didi

c++ - 声明一个 std::map 迭代器会导致一个奇怪的错误

转载 作者:太空狗 更新时间:2023-10-29 23:34:07 24 4
gpt4 key购买 nike

我只是想声明一个映射迭代器,但我收到一个编译错误,提示“expected ; before it”

我相信这是因为我没有包含整个 std 命名空间(using namespace std;)但我有意不想包含所有内容。

我的代码:

#include <map>
#include <string>

template <class Object>
class Cont
{
public:
Cont() {}
Object* get( unsigned int nID )
{
std::map <unsigned int, Object*>::iterator it = m.begin(); // error here "expected ; before it" what is this error?

for ( ; it != m.end(); it++ )
{
if ( (*it).second->ID == nID ) { return (*it).second; }
}

return NULL;
}

std::map <unsigned int, Object*> m;
};

我也试过了,但是没用:

std::map <unsigned int, Object*>::std::iterator it = m.begin();

最佳答案

如果我没记错的话,因为您使用的是模板参数,您需要在迭代器声明前加上 typename 前缀。

typename std::map <unsigned int, Object*>::iterator it = m.begin();

关于c++ - 声明一个 std::map 迭代器会导致一个奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6000004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com