Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从基类调用方法,其中基类是模板参数 我有一个C++类,就像下面的一个在VC++中工作,但是在Linux GCC 4.7中不再工作。我不知道如何让它再次发挥作用_C++ - Fatal编程技术网

如何从基类调用方法,其中基类是模板参数 我有一个C++类,就像下面的一个在VC++中工作,但是在Linux GCC 4.7中不再工作。我不知道如何让它再次发挥作用

如何从基类调用方法,其中基类是模板参数 我有一个C++类,就像下面的一个在VC++中工作,但是在Linux GCC 4.7中不再工作。我不知道如何让它再次发挥作用,c++,C++,测试h template<typename a> class test: public a { public: void fun(); }; 模板_class.cpp #include "templateclass.h" template_class::template_class() { // TODO Auto-generated constructor stub } template_class::~template_class() { // T

测试h

template<typename a>
class test: public a
{
public:
    void fun();
};
模板_class.cpp

#include "templateclass.h"

template_class::template_class() {
    // TODO Auto-generated constructor stub

}

template_class::~template_class() {
    // TODO Auto-generated destructor stub
}

void template_class::template_class_method() {
}

您需要使用基类名称将其限定为:

a::template_class_method();
由于
a
中存在
template\u class\u method
,因此有必要进行限定
a::
。C++规则是,如果基是类模板或模板参数,那么它的所有成员都是强的>不< /强>派生类自动可见。为了帮助编译器找到该成员,您需要告诉它在基类中查找该成员,为此您需要限定
base::member\u function()
base::member\u data
形式的成员

在您的情况下,由于基是
a
,而成员是
模板\类\方法
,因此您必须编写以下代码:

a::template_class_method();

请注意,这样的基类称为依赖基类,因为它依赖于模板参数。

我不知道为什么@Karthik没有删除答案,但答案是正确的。你有几个选择

  • 使用限定名
    a::template\u class\u method()

  • 通过使用声明使基类方法可见

    template<typename a>
    class test : public a
    {
    public:
      using a::template_class_method;
      void fun()
      {
        template_class_method();
      }
    };
    
    模板
    班级考试:公共a
    {
    公众:
    使用::template\u class\u方法;
    虚无乐趣()
    {
    模板_类_方法();
    }
    };
    
  • 请注意,第一种方法将抑制
    模板类方法的虚拟性(在虚拟的情况下),因此应谨慎使用。因此,首选方法2,因为它保留了
    模板\u类\u方法的自然行为


    你的评论说
    这个->模板类方法()
    “不起作用”是不清楚的。它工作没有任何问题。此外,正如我上面所说的,在一般情况下,这是一个比使用限定名称更好的选择。

    不再有效,意味着您会遇到编译器错误?让他们看看我的坏。错误-没有依赖于模板参数的“template\u class\u method”参数,因此“template\u class\u method”的声明必须可用[-fpPermissive]。@BryanFok:
    base
    当然不是名称。你需要写一个
    a
    。我出错了。“template\u class\u method”没有依赖于模板参数的参数,因此“template\u class\u method”的声明必须可用[-fppermissive]@BryanFok:这是另一个问题。现在你需要显示更多的代码,相关的代码,比如你作为模板参数传递给
    test
    的是什么,以及你是如何定义这个类型的?@BryanFok:你需要编写
    模板类test
    模板的类定义之后,而不是在它之前。我很抱歉。那只是我在帖子上的编辑错误。我相信错误与此无关。
    
    a::template_class_method();
    
    template<typename a>
    class test : public a
    {
    public:
      void fun()
      {
        a::template_class_method();
      }
    };
    
    template<typename a>
    class test : public a
    {
    public:
      void fun()
      {
        this->template_class_method();
      }
    };
    
    template<typename a>
    class test : public a
    {
    public:
      using a::template_class_method;
      void fun()
      {
        template_class_method();
      }
    };