【发布时间】:2013-12-22 18:33:49
【问题描述】:
我在编译代码时遇到错误,如下所示:
#include<iostream>
template <class T>
class A
{
protected:
T protectedValue;
template<class TT>
class insideClass
{
public:
TT insideClassValue;
};
};
template<class T>
class B : public A<T>
{
public:
void print(T t)
{
insideClass<T> ic; // <-- the problem should be here
ic.insideClassValue = t;
std::cout << ic.indideClassValue << std::endl;
};
};
int main()
{
double v = 2.;
B<double> b;
b.print(v);
return 0;
};
编译器 (g++) 给出以下错误:
main.C: In member function ‘void B<T>::printA()’:
main.C:23:4: error: ‘insideClass’ was not declared in this scope
main.C:23:17: error: expected primary-expression before ‘>’ token
main.C:23:19: error: ‘ic’ was not declared in this scope
我发现如果A类不是模板类,编译不会报错。 我不明白为什么将 A 类设为模板类会导致所描述的错误。 知道原因以及如何解决问题吗?
【问题讨论】:
标签: c++ templates inheritance