質問

私は、C ++を使用したオプション価格設定に関する本である「C ++を使用したFinancial Instrument Pricing」のC ++コードを使用しています。次のコードには、基本的に定義しようとする多くの詳細が剥がれた小さなスニペットがあります 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

私がここで間違っている、または忘れている愚かなものや基本的なものはありますか?構文エラーですか?指を置くことはできません。このコードスニペットが撮影された本は、彼らのコードがVisual Studio 6にコンパイルされたと言っています。これはバージョン関連の問題ですか?

ありがとう。

役に立ちましたか?

解決

コンパイラが示すように、次のことを交換する必要があります。

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

見る このリンク 依存名の説明について。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top