2010-11-27 96 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