模板错误:未在范围中声明Iterator

时间:2011-10-03 11:37:46

标签: 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_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;
            }

          }
        };
    };

这是无法编译的方法之一(类的所有方法都在同一个迭代器中失败)。此代码适用于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

欢迎任何帮助

1 个答案:

答案 0 :(得分:6)

声明如下:

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

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