1 #include <iostream>
2 using namespace std;
3 template<typename T>
4 class Top {
5 public:
6 class Sub {
7 protected:
8 T age;
9 };
10 class Derived : public Sub {
11 public:
12 void printAge() {
13 cout << age << endl;
14 }
15 };
16 };
17 int main()
18 {
19 return 0;
20 }
當我complie代碼,我得到以下錯誤:模板類已經嵌套類
test.cpp: In member function ‘void Top<T>::Derived::printAge()’:
test.cpp:13:25: error: ‘age’ was not declared in this scope
cout << age << endl;
但是,如果它不是一個模板,這將是確定。 我很高興收到你的答案。
用cout << Sub :: age << endl;替換'cout << age << endl;'適用於我。不知道我可以解釋它! –
適用於VS2010 –
「this-> age」也可以工作 – user1233963