#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;
}
編譯器返回錯誤:C++繼承
main.cpp中:在成員函數 '虛擬無效CThirdLevel ::則PushBack(T)':
main.cpp中: 32:37:錯誤:'數據'未在此範圍內聲明
問題在哪裏?有可能使用這個繼承嗎?
HTTP:// WWW。 parashift.com/c++-faq-lite/nondependent-name-lookup-members.html – aschepler
[在C++中使用模板訪問超類的受保護成員]的可能副本(http://stackoverflow.com/questions/4010281/accessing-protected-members-of-superclass-in-c-with-templates) – jogojapan