2010-11-27 93 views
0

我正在通過「金融工具定價使用C++」中的一些C++代碼 - 一本關於使用C++的期權定價的書。下面的代碼是一小段摘錄了許多細節,基本上試圖定義一個旨在包含名稱和列表的SimplePropertySet類。STL的編碼迭代器函數

#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 
} 

在編制上VS2008這個代碼,我得到了以下錯誤:

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 

有一些愚蠢的或基本的,我得到錯誤或忘記在這裏?這是一個語法錯誤?我無法指責它。從這個代碼片段中讀取的書說他們的代碼是在Visual Studio 6上編譯的。這是否與版本相關的問題?

謝謝。

回答

2

由於編譯器指示,則必須更換:

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

有:

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

對相關名稱的說明,請參見this link

+0

哦......那麼簡單......謝謝你指出我在正確的方向,我的愚蠢問題分開。你的建議工作得很好。謝謝。 – Tryer 2010-11-27 19:19:48