संभावित डुप्लिकेट:
[FAQ]Why doesn't a derived template class have access to a base template class' identifiers? Problem with protected fields in base class in c++
cannot access data member in a class templateसी ++ टेम्पलेट वर्ग और विरासत
कोड के बाद मुझे संकलन त्रुटि देता है। गलत क्या है?
struct Base {
int amount;
};
template<class T> struct D1 : public Base {
};
template<class T>
struct D2 : D1<T> {
void foo() { amount=amount*2; /* I am trying to access base class data member */ };
};
int main() {
D2<int> data;
};
test.cpp: In member function 'void D2<T>::foo()':
test.cpp:11: error: 'amount' was not declared in this scope
इसे कैसे ठीक करें?
धन्यवाद
मैंने इस प्रश्न को कई बार पहले देखा है लेकिन मुझे कोई लिंक नहीं मिल रहा है। –
एक मिला, हालांकि अगर कोई एक बेहतर प्रश्न के साथ एक को ढूंढ सकता है जो बहुत अच्छा होगा: [सी ++ में बेस क्लास में संरक्षित फ़ील्ड में समस्या] (http://stackoverflow.com/questions/1813671/problem-with- संरक्षित- फ़ील्ड-इन-बेस-क्लास-इन-सी) –
@ क्रिस: यहां एक [डुप्लिकेट] है (http://stackoverflow.com/questions/4210108/cannot-access-data-member-in-a-class-template) , और यहां एक [लंबा स्पष्टीकरण] है (http://stackoverflow.com/questions/4643074/why-do-i-have-to-access-template-base-class-members-through-the-this- सूचक)। – GManNickG