【问题标题】:Coding iterator function for STL ClassSTL 类的编码迭代器函数
【发布时间】:2010-11-27 19:03:44
【问题描述】:

我正在研究“使用 C++ 进行金融工具定价”中的一些 C++ 代码——一本关于使用 C++ 进行期权定价的书。以下代码是一个去除了许多细节的小 sn-p,它基本上试图定义一个旨在包含名称和列表的 SimplePropertySet 类。

#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

我在这里犯了错误或忘记了一些愚蠢或基本的事情吗?是语法错误吗?我无法将手指放在它上面。获取此代码 sn-p 的书说他们的代码是在 Visual Studio 6 上编译的。这是与版本相关的问题吗?

谢谢。

【问题讨论】:

    标签: c++ syntax quantitative-finance computational-finance


    【解决方案1】:

    根据编译器的指示,您必须替换:

    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 了解有关依赖名称的说明。

    【讨论】:

    • 哦...这么简单...感谢您指出我正确的方向,我的愚蠢问题分开了。你的建议效果很好。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 2017-04-23
    相关资源
    最近更新 更多