3

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> 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;
            }

          }
        };
    };

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

Killrazor
  • 6,856
  • 15
  • 53
  • 69

1 Answers1

6

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).

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • And to complete: Visual Studio is completely broken when it comes to template, and accept invalid code. gcc and Clang are less broken, though they have ground to cover when it comes to name look-up :x – Matthieu M. Oct 03 '11 at 12:16
  • 1
    @MatthieuM.: Thanks! I have very little experience with MSVC (on account of MSVCExpress demanding 2GB of my precious C: partition, non-relocatably!), so I'm always happy to learn about common compiler idiosyncrasies. How is GCC broken in terms of name lookup? – Kerrek SB Oct 03 '11 at 12:18
  • 1
    see a [question on name lookup](http://stackoverflow.com/questions/7630806/the-actual-result-of-name-resolution-in-the-class-template-is-different-from-the), gcc and clang miscompile an example straight from the Standard :x – Matthieu M. Oct 03 '11 at 12:40
  • Thank you so much both of you. After your response I successfully found much more information, and a useful link about "Two-phase name lookup" here http://womble.decadent.org.uk/c++/template-faq.html#two-phase – Killrazor Oct 03 '11 at 13:22