c++ syntax: iterator fuer struct in template Klasse

R

rikola

Foren Gott
Hallo,

ich versuche, fuer eine template-Klasse, die ein struct enthaelt, eine iterator zu deklarieren, doch laut g++ bekomme ich die Syntax nicht hin:
Code:
#include <vector>

template <typename T> class A{
    private:
        struct B {
            float x;
        };
        B b;
    public:
        A(float x){b.x = x;}
        ~A(){}
        void f();
};

template <class T> void A<T>::f() {
    //std::vector<B>::iterator bs; //<- g++ schlaegt hier folgendes vor:
    typename std::vector<A<T>::B,std::allocator<A<T>::B> >::iterator it;
}

int main()
{
    A<float> a(3.2);
    return 0;
}

Der Code funktioniert, solange ich in f() den iterator nicht deklariere. Mit der auskommentierten Syntax schlaegt g++ die im momentan angegebene Syntax zwar vor, ist damit aber auch nicht zufrieden:
Code:
templ.cpp: In member function 'void A<T>::f()':
templ.cpp:19: error: type/value mismatch at argument 1 in template parameter list for 'template<class _Alloc> class std::allocator'
templ.cpp:19: error:   expected a type, got 'A<T>::B'
templ.cpp:19: error: type/value mismatch at argument 1 in template parameter list for 'template<class _Tp, class _Alloc> class std::vector'
templ.cpp:19: error:   expected a type, got 'A<T>::B'
templ.cpp:19: error: template argument 2 is invalid
templ.cpp:19: error: invalid type in declaration before ';' token
templ.cpp: In member function 'void A<T>::f() [with T = float]':
templ.cpp:29:   instantiated from here
templ.cpp:19: warning: unused variable 'it'
Koennte mir jemand die korrekte Syntax verraten? Der intel-compiler akzeptier die auskommentierte Version, doch laut c++ FAQ ist das nicht Standardkonform.
 
Kann es sein das du ein ">" vergessen hast???

z.B.:

template <class T> void A<T>::f() {
//std::vector<B>::iterator bs; //<- g++ schlaegt hier folgendes vor:
typename std::vector<A<T>>::B,std::allocator<A<T>::B> >::iterator it;
}

oder

template <class T> void A<T>::f() {
//std::vector<B>::iterator bs; //<- g++ schlaegt hier folgendes vor:
typename std::vector<A<T>::B>
,std::allocator<A<T>::B> >::iterator it;
}
 
PHP:
template <typename T> void A<T>::f() {
   typename std::vector<typename A::B>::iterator bs;
}

GCC (bei mir 4.2.1) kommt bei diesem Konstrukt bei der automatischen Typerkennung durcheinander und deshalb müssen die Typen an beiden Stellen mit typename und der innere Typ A::B mit voll qualifiziertem Namen markiert werden.

Der Microsoft C++-Compiler akzeptiert Dein Ausgangskonstrukt übrigens auch ohne Murren.
 
Zuletzt bearbeitet:

Ähnliche Themen

Aufgabe in C

Rollei Mini Wifi Camcorder

g++ kompilieren schlägt fehl

Festplatte friert ein nach suspend/resume

TrueCrypt 5.0a installieren

Zurück
Oben