C ++继承的模板类无权访问基类 [英] C++ Inherited template classes don't have access to the base class
本文介绍了C ++继承的模板类无权访问基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我第一次使用模板类,并尝试找出为什么编译器似乎不喜欢当我使用继承。
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.
这里是代码:
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
谢谢
推荐答案
通过使用 this-> x
和 this-> y
来帮助编译器。
You need to help the compiler out by using this->x
and this->y
.
http://www.parashift.com/c++- faq / templates.html#faq-35.19
这篇关于C ++继承的模板类无权访问基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文