[英]Inheriting from template abstract class
I have this code structure:我有这个代码结构:
class A
{
protected:
int u;
int z;
public:
A(int uu,
int zz)
:u(uu),
z(zz)
{};
int get() const
{
return u;
};
virtual void blah() = 0;
};
template <class T>
class B : public A
{
protected:
std::vector<T> xxxx;
public:
B(int uu,
int zz,
int bbb)
:A(uu,
zz)
{
for(size_t i = 0; i < bbb; i ++)
xxxx[i] = 0;
};
virtual void blah()
{
u = u + 1;
};
};
template <class T>
class C : public B<T>
{
protected:
int qrqr;
public:
C(int uu,
int zz,
int bbb,
int qrqrqr)
:B<T>(uu,
zz,
bbb),
qrqr(qrqrqr)
{
};
virtual void blah()
{
u = u + qrqr;
};
};
When I compile I get this error:当我编译时,我收到此错误:
error: 'u' was not declared in this scope
at line: u = u + qrqr;
Though u
is clearly an element of C because A is a base of C through B.尽管u
显然是 C 的元素,因为 A 是 C 到 B 的基础。
I read here: Class template inheritance C++ that the proper way to inherit from class template is either to specialize the class or to have the inherited class to be template and this is what I did here.我在这里读到: 类模板继承 C++ ,从类模板继承的正确方法是专门化类或将继承的类作为模板,这就是我在这里所做的。 The compiler doesn't seem to complain about it anyway.无论如何,编译器似乎并没有抱怨它。 Could it be because A is an abstract class?可能是因为 A 是一个抽象类?
What did I do wrong?我做错了什么?
Thanks!谢谢!
This is a problem with a nondependent name - the member you're referring to does not depend on the template parameter.这是非依赖名称的问题 - 您所指的成员不依赖于模板参数。 The compiler does not look in dependent base classes (like B<T>
) when looking up nondependent names (like u
).在查找非依赖名称(如u
)时,编译器不会查找依赖基类(如B<T>
)。
You can solve it by using您可以通过使用解决它
this->u = this->u + qrqr;
which specifies to the compiler which u
you mean.它指定了编译器u
你的意思。
There are at least two other ways, by calling B<T>::u
, or writing using B<T>::u;
至少还有两种其他方式,通过调用B<T>::u
,或using B<T>::u;
编写using B<T>::u;
in the function before this line.在此行之前的函数中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.