C++继承的模板类无权访问基类

Sha*_*ley 1 c++ inheritance templates

我是第一次使用模板类,并试图找出编译器在使用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; };
};
Run Code Online (Sandbox Code Playgroud)

编译器输出:

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
Run Code Online (Sandbox Code Playgroud)

是不是可以这样使用模板?

谢谢

asc*_*ler 8

您需要使用this->x和帮助编译器this->y.

http://www.parashift.com/c++-faq/templates.html#faq-35.19