Iterator in abgeleiteter Template Klasse benutzen



  • Ich habe eine Klasse MyMap von std::map angeleitet. Die beiden template arguments habe ich beibehalten.

    #include <map>
    using namespace std;
    
    template<typename T, typename T0>
    class MyMap : public std::map<T,T0>
    {
    public:
        void foo();
    
    };
    
    template<typename T, typename T0>
    void MyMap<T,T0>::foo()
    {
    
        for(iterator it(begin()); it!=end(); ++it)   //<-- compile time error
            it->bla();
    
        //klappt auch nicht:
        //for(MyMap<T,T0>::iterator it(begin()); it!=end(); ++it)
        //    it->bla();
    }
    

    Es gibt Probleme mit dem statement: iterator it(begin())

    Any help?

    thx in advance



  • for(typename MyMap<T,T0>::iterator it(begin()); it!=end(); ++it)
    

    So funktionierts, siehe dazu: http://www.slac.stanford.edu/BFROOT/www/Computing/Programming/GCCWarnings.html



  • Na, das is ja mal ein Ding

    this is because the C++ standard rule is that every identifier that involves a template is interpreted as a value unless it is explicitly qualified as a typename. Thus, when you write;

    vector<T>::const_iterator i;

    const_iterator may be interpreted as a static member of the class vector<T>. To make it clear that it is the type that declares the value i, one needs to write;

    typename vector<T>::const_iterator i;

    Da waere ich auch nicht von alleine drauf gekommen..
    Vielen Dank!



  • Raptor schrieb:

    Na, das is ja mal ein Ding

    this is because the C++ standard rule is that every identifier that involves a template is interpreted as a value unless it is explicitly qualified as a typename. Thus, when you write;

    vector<T>::const_iterator i;

    const_iterator may be interpreted as a static member of the class vector<T>. To make it clear that it is the type that declares the value i, one needs to write;

    typename vector<T>::const_iterator i;

    Da waere ich auch nicht von alleine drauf gekommen..
    Vielen Dank!

    Wenn du dich jetzt eventuell noch fragst, warum? Dann ist dieser Link imho ebenfalls hilfreich: http://www.gotw.ca/gotw/035.htm.

    Gruß Caipi


Anmelden zum Antworten