[英]c++ issue with template class inheritance
我在编译可以轻视的代码时遇到错误,如下所示:
#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作为模板类会导致所描述的错误。 有关原因以及如何解决问题的任何想法?
像这样的东西:
typedef typename A<T>::template insideClass<T> ic;
public:
void print(T t)
{
ic ic;
ic.insideClassValue = t;
std::cout << ic.insideClassValue << std::endl;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.