سؤال

أعمل من خلال بعض كود 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