开发者

Error in template: Iterator was not declared in the scope

开发者 https://www.devze.com 2023-04-09 23:26 出处:网络
trying to make a template, but I have an error in gcc4 but not in VS2008. This is the code that fails:

trying to make a template, but I have an error in gcc4 but not in VS2008. This is the code that fails:

#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> g开发者_运维百科etKeys()
            {
                    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;
            }

          }
        };
    };

This is one of the methods that fails to compile (all methods of the class fail in the same iterator). This code works fine into visual studio but compilation in GCC return this error:

/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

Any help will be welcome


Declare it like this:

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

The point is that boost::ptr_map<Key,Value>::iterator is a dependent name, so you have to specify that it's a type name (and not a variable name or a template name).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号