C++ 模板中出错:未在范围中声明迭代器

C++ 模板中出错:未在范围中声明迭代器,c++,templates,C++,Templates,正在尝试制作模板,但我在gcc4中有一个错误,但在VS2008中没有。这是失败的代码: #pragma once #ifndef _ZELESTE_ZEL_GPX_GENERALMANAGER_H_ #define _ZELESTE_ZEL_GPX_GENERALMANAGER_H_ #include "../internal/cuCoreLib.h" #include "boost/functional/hash.hpp" #include "boost/ptr_container/ptr_

正在尝试制作模板,但我在gcc4中有一个错误,但在VS2008中没有。这是失败的代码:

#pragma once
#ifndef _ZELESTE_ZEL_GPX_GENERALMANAGER_H_
#define _ZELESTE_ZEL_GPX_GENERALMANAGER_H_

#include "../internal/cuCoreLib.h"
#include "boost/functional/hash.hpp"
#include "boost/ptr_container/ptr_map.hpp"

namespace z3d{
    namespace core{
        template<class Key, class Value>
        class cuManager
        {
            boost::ptr_map<Key, Value> m_ItemMap;

        public:
            /**
            * Default constructor
            */
            cuManager(void){}

            /**
            * Get a vector that contain the keys of the elements contained in the manager.
            * @return an const std::vector of type Key 
            */
            const std::vector<Key> getKeys()
            {
                    boost::ptr_map<Key,Value>::iterator itr = m_ItemMap.begin();
                    std::vector<Key> v;
                    while(itr != m_ItemMap.end())
                    {
                            v.push_back(itr->first);
                            ++itr;
                    }
                    return v;
            }

          }
        };
    };
#pragma一次
#ifndef_ZELESTE_ZEL_GPX_GENERALMANAGER_H_
#定义_ZELESTE_ZEL_GPX_GENERALMANAGER_H_
#包括“./internal/cuCoreLib.h”
#包括“boost/functional/hash.hpp”
#包括“boost/ptr_container/ptr_map.hpp”
名称空间z3d{
名称空间核心{
模板
班级管理员
{
boost::ptr_-map m_-ItemMap;
公众:
/**
*默认构造函数
*/
cuManager(void){}
/**
*获取一个向量,该向量包含管理器中包含的元素的键。
*@return一个Key类型的const std::vector
*/
const std::vector getKeys()
{
boost::ptr_map::迭代器itr=m_ItemMap.begin();
std::向量v;
while(itr!=m_ItemMap.end())
{
v、 向后推(itr->first);
++itr;
}
返回v;
}
}
};
};
这是无法编译的方法之一(类的所有方法都在同一迭代器中失败)。此代码在visual studio中运行良好,但在GCC中编译会返回以下错误:

/home/dev001/desarrollo/code/lpgameengine/LPGameEngine/src/core/datatypes/cuManager.h: In member function ‘const std::vector<_Tp, std::allocator<_CharT> > z3d::core::cuManager<Key, Value>::getKeys()’:
/home/dev001/desarrollo/code/lpgameengine/LPGameEngine/src/core/datatypes/cuManager.h:28: error: expected ‘;’ before ‘itr’
/home/dev001/desarrollo/code/lpgameengine/LPGameEngine/src/core/datatypes/cuManager.h:30: error: ‘itr’ was not declared in this scope
/home/dev001/desarrolo/code/lpgameengine/lpgameengine/src/core/datatypes/cuManager.h:在成员函数“const std::vector z3d::core::cuManager::getKeys()”中:
/home/dev001/desarrolo/code/lpgameengine/lpgameengine/src/core/datatypes/cuManager.h:28:错误:预期为“;”在“itr”之前
/home/dev001/desarrolo/code/lpgameengine/lpgameengine/src/core/datatypes/cuManager.h:30:错误:未在此范围内声明“itr”
欢迎提供任何帮助

如下所示:

typename boost::ptr_map<Key,Value>::iterator itr = m_ItemMap.begin();
^^^^^^^^
typename boost::ptr_map::迭代器itr=m_ItemMap.begin();
^^^^^^^^

关键是,
boost::ptr_map::iterator
是一个依赖名称,因此您必须指定它是一个类型名(而不是变量名或模板名)。

并且要完成:Visual Studio在使用模板时完全崩溃,并接受无效代码。gcc和Clang的破坏性较小,但在名称查找方面,它们有足够的理由:x@MatthieuM.:谢谢!我对MSVC几乎没有经验(因为MSVCExpress需要2GB宝贵的C:分区,不可重新定位!),所以我总是乐于了解常见的编译器特性。GCC在名称查找方面是如何被打破的?请看一个,GCC和clang miscompile,一个直接来自标准的例子:xThank you some you two you。在您的回复之后,我成功地找到了更多的信息,以及关于“两阶段名称查找”的有用链接