【发布时间】:2019-10-11 20:49:11
【问题描述】:
我有以下代码:https://gist.github.com/PatrikValkovic/50329975f86e0328ff1f85fda17a23f3,此处为实时示例:http://cpp.sh/675a3。
简而言之,我有内部类 B 的类 A 和应该从 B 继承的类 D。当类 D 在类 A 中声明时(如注释),该代码有效。但是,当我将声明移到 A 类之外(如示例中所示)时,编译器会抱怨 A<T>::B<U> 不是类型。语法有什么问题?
// Example program
#include <iostream>
#include <string>
using namespace std;
template<typename T>
class A
{
public:
template<typename U>
class B;
/*
class D : public B<int>
{
public:
void method2() {
cout << "Method 2" << endl;
this->method1();
}
};
*/
class D;
};
template<typename T>
template<typename U>
class A<T>::B
{
public:
void method1() {
T x;
cout << "Method 1: " << x << endl;
}
};
template<typename T>
class A<T>::D : public A<T>::B<int>
{
public:
void method2() {
cout << "Method 2" << endl;
this->method1();
}
};
int main()
{
A<int>::D b;
b.method2();
}
【问题讨论】:
标签: c++ c++11 templates inheritance