Вопрос

Я работаю через какой-то код C ++ из «Цены финансового инструмента, используя C ++» - книгу по опционной цене, используя 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