لماذا لا يعمل هذا النوع من النوع؟ (C ++)
-
23-08-2019 - |
سؤال
عندما أحاول استخدام فئة مكررتي
template<class T>
class list
{
public:
class iterator;
};
template<class T>
class list<T>::iterator
{
//stuff
};
كوعي عودة في التحميل الزائد للمشغل،
template<class T>
class list<T>::iterator
{
public:
iterator& operator++();
protected:
list* lstptr;
};
template<class T>
iterator& list<T>::iterator::operator++()
{
(this->lstptr)->current = ((this->lstptr)->current)->next;
return this;
}
أحصل على هذه الأخطاء:
s:\jeffrey_\my_freeware_games\o\resources\container class\container(spec- o)\container_def.h(213) : error C2143: syntax error : missing ';' before '&'
s:\jeffrey_\my_freeware_games\o\resources\container class\container(spec- o)\container_def.h(213) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
s:\jeffrey_\my_freeware_games\o\resources\container class\container(spec- o)\container_def.h(213) : error C2065: 'T' : undeclared identifier
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(235) : error C2039: 'Yes' : is not a member of 'vc_attributes'
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(235) : error C2065: 'Yes' : undeclared identifier
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(236) : error C2039: 'No' : is not a member of 'vc_attributes'
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(236) : error C2065: 'No' : undeclared identifier
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(237) : error C2039: 'Maybe' : is not a member of 'vc_attributes'
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(237) : error C2065: 'Maybe' : undeclared identifier
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(240) : error C2039: 'NoAccess' : is not a member of 'vc_attributes'
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(240) : error C2065: 'NoAccess' : undeclared identifier
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(241) : error C2039: 'Read' : is not a member of 'vc_attributes'
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(241) : error C2065: 'Read' : undeclared identifier
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(242) : error C2039: 'Write' : is not a member of 'vc_attributes'
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(242) : error C2065: 'Write' : undeclared identifier
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(243) : error C2039: 'ReadWrite' : is not a member of 'vc_attributes'
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(243) : error C2065: 'ReadWrite' : undeclared identifier
c:\program files\microsoft visual studio 9.0\vc\include\crtdefs.h(582) : error C2146: syntax error : missing ';' before identifier 'time_t'
c:\program files\microsoft visual studio 9.0\vc\include\crtdefs.h(2047) : error C2143: syntax error : missing ';' before 'identifier'
c:\program files\microsoft visual studio 9.0\vc\include\crtdefs.h(2047) : warning C4091: 'typedef ' : ignored on left of 'localeinfo_struct' when no variable is declared
c:\program files\microsoft visual studio 9.0\vc\include\crtdefs.h(2047) : fatal error C1075: end of file found before the left brace '{' at 'c:\program files\microsoft visual studio 9.0\vc\include\crtdefs.h(174)' was matched
NB: Coailer_Def.H هو ملف الرأس لفئات قائمتي ودروس ITERATCER، ليس لدي أي فكرة عن ما هي SouceAnnotations أو CRTDEFS.
المحلول
iterator
لم يعرف بعد في تلك المرحلة. تحتاج إلى معرفة ذلك في list<T>
صف دراسي:
template<class T>
typename list<T>::iterator& list<T>::iterator::operator++() {
(this->lstptr)->current = ((this->lstptr)->current)->next;
return *this; // *this here, since this is a pointer only
}
لاحظ ال typename
مطلوب، منذ list<T>::iterator
هو نوع مسبق مع تخصص القالب، وتحتاج إلى إخبار المحول البرمجي حول ذلك - على الرغم من حقيقة أن Visual C ++ ربما سيقبل الكود لا يضع typename
قبل ذلك. حذف typename
, ، يجب أن يفترض المحول البرمجي أنه ليس نوعا ويجب عليه نوع من إنتاج رسالة الخطأ نفسها.
يمكنك آمنة نفسك أن المتاعب من خلال وضع التعليمات البرمجية مباشرة في الفصل:
template<class T>
class list<T>::iterator
{
public:
iterator& operator++() {
(this->lstptr)->current = ((this->lstptr)->current)->next;
return *this; // *this here, since this is a pointer only
}
protected:
list* lstptr;
};
نصائح أخرى
Litb has. أجاب سؤالك تماما. أعتقد أنه يستحق التسليط الضوء على أنه في محاولة لجعل C ++ أسهل في الاستخدام، أضافت لجنة C ++ بناء جملة جديد لإعلان الوظائف. والنتيجة هي أنك ستتمكن من تحديد وظيفتك على النحو التالي (N2541.) دون الحاجة إلى التأهيل الإضافي:
template<class T>
auto list<T>::iterator::operator++()->iterator&
{
// ...
}
وفقا للميزة المدعومة قائمة, ، GCC 4.4 لديها بالفعل هذه الميزة.