#include <cstdlib>
#include <vector>
#include <iostream>
using namespace std;
class CFirstLevel {
public:
CFirstLevel (const string & _name): name (_name) {}
// ...
protected:
string name;
};
template <typename T>
class CSecondLevel: public CFirstLevel {
public:
CSecondLevel (const string & _name): CFirstLevel (_name) {}
virtual void PushBack (T) = 0;
virtual void Print (int I) {cout << data [I] << endl;}
// ...
protected:
vector<T> data;
};
template <typename A>
class CThirdLevel: public CSecondLevel<A> {
public:
CThirdLevel (const string & _name): CSecondLevel<A> (_name) {}
virtual void PushBack (A _value) {data.push_back (_value);}
};
int main (void) {
CThirdLevel<int> * pointer = new CThirdLevel<int> ("third");
pointer -> PushBack (111);
pointer -> Print (0);
return 0;
}
biên dịch lại lỗi:C++ thừa kế của mẫu vật (G ++)
main.cpp: Trong chức năng thành viên 'virtual void CThirdLevel :: PUSHBACK (T):
main.cpp: 32:37: lỗi: 'dữ liệu' không được khai báo trong phạm vi này
Trường hợp xảy ra sự cố? Có thể sử dụng thừa kế này?
http: // www. parashift.com/c++-faq-lite/nondependent-name-lookup-members.html – aschepler
có thể trùng lặp của [truy cập các thành viên được bảo vệ của siêu lớp trong C++ với các mẫu] (http://stackoverflow.com/questions/4010281/accessing-protected-members-of-superclass-in-c-with-templates) – jogojapan