C++继承的模板类无权访问基类
我第一次使用模板类,并试图找出为什么编译器似乎不喜欢我使用继承。
这是代码:
template <typename T>
struct xPoint2
{
T x;
T y;
xPoint2() { x = 0; y = 0; };
};
template <typename T>
struct xVector2 : xPoint2<T>
{
xVector2() { x = 0; y = 0; };
};
编译器输出:
vector2.hh: In constructor ‘xVector2<T>::xVector2()’:
vector2.hh:11: error: ‘x’ was not declared in this scope
vector2.hh:11: error: ‘y’ was not declared in this scope
难道不能以这种方式使用模板吗?
谢谢
I am working with template classes for the first time, and trying to figure out why the compiler doesn't seem to like when I use inheritence.
Here is the code:
template <typename T>
struct xPoint2
{
T x;
T y;
xPoint2() { x = 0; y = 0; };
};
template <typename T>
struct xVector2 : xPoint2<T>
{
xVector2() { x = 0; y = 0; };
};
The compiler output:
vector2.hh: In constructor ‘xVector2<T>::xVector2()’:
vector2.hh:11: error: ‘x’ was not declared in this scope
vector2.hh:11: error: ‘y’ was not declared in this scope
Is it not possible to use templates in this way?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用
this->x
和this->y
来帮助编译器。http://www.parashift.com/c++-faq/templates.html #faq-35.19
You need to help the compiler out by using
this->x
andthis->y
.http://www.parashift.com/c++-faq/templates.html#faq-35.19
您必须明确引用父级:
You must explicitly refer to parent: