Fehler bei const_iterator



  • Folgender Code

    #include <vector>
    #include <algorithm>
    
    namespace ArraysFunctions
    {  
        template<typename T>	int maxPosition(vector<T> const & InArray);
    }
    
    template<typename T>
    inline int ArraysFunctions::maxPosition(vector<T> const & InArray)
    {
        vector<T>::const_iterator it = std::max_element( InArray.begin(), InArray.end() );
        return int(std::distance(InArray.begin(), it));
    }
    

    liefert bei mir den Fehler:

    In file included from QSpectrogramData.h:16,
    from QSpectrogramPlot.cpp:3:
    ..\include/arrayfunctions.h: In function 'int ArraysFunctions::maxPosition(const std::vector<T, std::allocator<_CharT> >&)':
    ..\include/arrayfunctions.h:133: error: expected ';' before 'it'
    ..\include/arrayfunctions.h:134: error: 'it' was not declared in this scope
    ..\include/arrayfunctions.h: In function 'int ArraysFunctions::minPosition(const std::vector<T, std::allocator<_CharT> >&)':
    ..\include/arrayfunctions.h:140: error: expected ';' before 'it'
    ..\include/arrayfunctions.h:141: error: 'it' was not declared in this scope

    Nur verstehe ich jetzt überhaupt nicht was an der Zeile mit dem Iterator falsch sein sollte.


  • Mod

    Versuch mal das:

    typename vector<T>::const_iterator it = std::max_element( InArray.begin(), InArray.end() );
    


  • Ich verstehe zwar nicht warum das die Lösung ist, aber jetzt kompiliert es.


  • Mod

    Darum braucht man typename:

    http://pages.cs.wisc.edu/~driscoll/typename.html

    Kurzzusammenfassung: Da T erstmal nicht bekannt ist, ist auch nicht bekannt, was vector<T>::const_iterator sein soll, aus der Syntax alleine geht nicht eindeutig hervor, dass es ein Typenbezeichner ist. Es könnte ja zum Beispiel auch eine statische Membervariable sein. Deshalb muss man hier mit dem Schlüsselwort typename kenntlich machen, dass es sich um einen Typenbezeichner handeln soll. Eine genauere Erklärung findet man im verlinkten Text.


Log in to reply