Frage

Ich arbeite durch einige C ++ Code aus „Finanzinstrument Preis C ++“ - ein Buch über Optionspreis mit C ++. Folgende Code ist ein kleiner Ausschnitt von vielen Details ausgezogen, die im Grunde versucht, eine SimplePropertySet Klasse zu definieren, die dazu bestimmt ist, einen Namen und eine Liste zu enthalten.

#include <iostream>
#include <list>
using namespace::std;

template <class N, class V> class SimplePropertySet
{
    private:
    N name;     // The name of the set
    list<V> sl;

    public:
    typedef typename list<V>::iterator iterator;
    typedef typename list<V>::const_iterator const_iterator;

    SimplePropertySet();        // Default constructor
    virtual ~SimplePropertySet();   // Destructor

    iterator Begin();           // Return iterator at begin of composite
    const_iterator Begin() const;// Return const iterator at begin of composite
};
template <class N, class V>
SimplePropertySet<N,V>::SimplePropertySet()
{ //Default Constructor
}

template <class N, class V>
SimplePropertySet<N,V>::~SimplePropertySet()
{ // Destructor
}
// Iterator functions
template <class N, class V>
SimplePropertySet<N,V>::iterator SimplePropertySet<N,V>::Begin()//<--this line gives error
{ // Return iterator at begin of composite
    return sl.begin();
}

int main(){
    return(0);//Just a dummy line to see if the code would compile
}

Auf diesen Code auf VS2008 kompilieren, ich die folgenden Fehler erhalten:

warning C4346: 'SimplePropertySet::iterator' : dependent name is not a type
    prefix with 'typename' to indicate a type
error C2143: syntax error : missing ';' before 'SimplePropertySet::Begin'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Gibt es etwas dumm oder grundlegend, dass ich falsch bin immer noch hier zu vergessen? Ist es ein Syntaxfehler? Ich bin nicht in der Lage zu meinen Finger auf sie. Das Buch, aus dem dieser Code-Schnipsel genommen wird, sagt ihr Code auf Visual Studio 6. Ist das ein versions verwandtes Problem kompiliert wurde?

Danke.

War es hilfreich?

Lösung

Wie der Compiler angegeben, müssen Sie ersetzen:

template <class N, class V>
SimplePropertySet<N,V>::iterator SimplePropertySet<N,V>::Begin()

mit:

template <class N, class V>
typename SimplePropertySet<N,V>::iterator SimplePropertySet<N,V>::Begin()

finden Sie unter für eine href="https://stackoverflow.com/questions/1527849/how-do-you-understand-dependent-names-in-c/1527910#1527910"> Erklärung abhängig Namen.

scroll top