2010-11-27 10 views
0

Ich arbeite durch einige C++ - Code aus "Financial Instrument Pricing Using C++" - ein Buch über die Option Preisgestaltung mit C++. Der folgende Code ist ein kleiner Auszug aus vielen Details, der grundsätzlich versucht, eine Klasse zu definieren, die einen Namen und eine Liste enthalten soll.Codierung Iterator-Funktion für STL-Klasse

#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 obtain:

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 kann meinen Finger nicht darauf legen. Das Buch, aus dem dieses Code-Snippet stammt, besagt, dass ihr Code in Visual Studio 6 kompiliert wurde. Handelt es sich um ein versionsbezogenes Problem?

Danke.

Antwort

2

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

Siehe this link für eine Erklärung auf abhängige Namen.

+0

Oh ... so einfach ... danke, dass du mich in die richtige Richtung weist, meine dumme Frage auseinander. Dein Vorschlag hat gut funktioniert. Danke. – Tryer

Verwandte Themen