模板类继承模板类

今天写的代码提交到OJ上就出现这样的错误,但是vs并不会出错。

'_elem' was not declared in this scope 

 原因在于模板类继承模板类,子类看不见父类的成员。

但是VC++做了一些小拓展,可以不适用this->就调用父类成员。

gcc在扫描到模板类时就要求确定每一个成员在哪里声明的,VC++在类实例化之后再检测,就不会有这个问题。

可以使用以下方式解决:

方法1:

使用this

template<typename T>
class A
{
protected:
    int x;
};

template<typename T>
class B:public A<T>
{
public:
    foo(this->x=0);
};

方法2:

使用A<T>::

template<typename T>
class A
{
protected:
    int x;
};

template<typename T>
class B:public A<T>
{
public:
    foo(A<T>::x=0);
};
原文地址:https://www.cnblogs.com/fei-hsueh/p/4539671.html