简体   繁体   English

C ++子类将字段的父类命名错误

[英]C++ child class mistaking parent class's name for a field

I'm trying to modify a C++ project for my own use and am having trouble creating a derived class. 我正在尝试修改我自己使用的C ++项目,并且在创建派生类时遇到问题。

When I have the constructor for the derived class call its parent's constructor, I get an error message saying that the derived class does not have a field corresponding to the name of its parent's class . 当我有派生类的构造函数调用其父的构造函数时,我收到一条错误消息,指出派生类没有与其父类的名称对应的字段 I don't understand where my problem lies. 我不明白我的问题在哪里。

The relevant parts of the .h file are as follows: .h文件的相关部分如下:

// grandparent_class ////////////////////////////////////////////////////////////////

template <typename T, typename Tds1 = T, typename Tds2 = T>
class grandparent_class : public module_1_1<T> {
 public:
  grandparent_class(uint nfeatures, const char *name = "grandparent_class");
  virtual ~grandparent_class();

// parent_class ////////////////////////////////////////////////////////////////

template <typename T, typename Tds1 = T, typename Tds2 = T>
class parent_class : public grandparent_class<T,Tds1,Tds2> {
 public:

  parent_class(uint nclasses, double target_factor = 1.0,
       bool binary_target = false,
       t_confidence conf = confidence_max,
       bool apply_tanh = false, const char *name = "parent_class",
       int force_class = -1, int single_output = -1,
       idxdim *kerd = NULL, double sigma_scale = 3,
                         bool silent = false);
  virtual ~parent_class();

  // members
  protected:
    int ctr;
};

// child_class (The Class I'm Introducing)////////////////////////////////////////////
template <typename T, typename Tds1 = T, typename Tds2 = T>
class child_class : protected parent_class<T,Tds1,Tds2> {
 public:
  child_class(uint nclasses, double target_factor = 1.0,
           bool binary_target = false,
           t_confidence conf = confidence_max,
           bool apply_tanh = false, const char *name = "child_class",
           int force_class = -1, int single_output = -1,
           idxdim *kerd = NULL, double sigma_scale = 3,
           bool silent = false);

virtual ~child_class();
};
//////////////////////////////////////////////////////////////////////////////

I define the constructor for my child class as follows: 我为我的子类定义构造函数,如下所示:

//////////////////////////////////////////////////////////////////////////////

template <typename T, typename Tds1, typename Tds2>
child_class<T,Tds1,Tds2>::
child_class(uint nclasses, double target_factor, bool binary_target_,
            t_confidence conf, bool apply_tanh_, const char *name_,
            int force, int single, idxdim *kerd, double sigma_scale,
                     bool silent)
    : parent_class(nclasses, target_factor, binary_target_,
         conf, apply_tanh_, name_,
         force, single, kerd, sigma_scale, silent)
  {
    ...
  };

My child class declaration and definition are in the same file as the parent & grandparent declaration and definition, so I don't think there's an issue of a missing "include". 我的子类声明和定义与父和祖父母声明和定义在同一个文件中,所以我认为没有遗漏“include”的问题。 As nearly as I can tell, my syntax for indicating inheritance is correct and I am calling the parent class's constructor correctly. 我几乎可以告诉我,我的表示继承的语法是正确的,我正确地调用了父类的构造函数。

Yet, when I compile, I get the following error: 然而,当我编译时,我收到以下错误:

/home/me/ebl_answer.hpp: In constructor ‘ebl::child_class<T, Tds1, Tds2>::child_class(uint, double, bool, ebl::t_confidence, bool, const char*, int, int, ebl::idxdim*, double, bool)’:
/home/me/ebl_answer.hpp:416:4: error: class ‘ebl::child_class<T, Tds1, Tds2>’ does not have any field named ‘parent_class’
  : parent_class(nclasses, target_factor, binary_target_,

If anyone can see my mistake it would be very helpful.... 如果有人能看到我的错误,那将是非常有帮助的....

In order to use inheritance, the class inherited from must be fully defined. 为了使用继承,必须完全定义继承自的类。 You can't inherit from a class template, as it is not a type, it is just something that will be used to stamp out (instantiate) a real type. 您不能从类模板继承,因为它不是类型,它只是用于标记 (实例化)实际类型的东西。 This is what you are doing in the declaration. 这就是你在声明中所做的。 Same goes for invoking the constructor of the base class. 同样适用于调用基类的构造函数。

Try using parent_class<T, Tds1, Tds2> 尝试使用parent_class<T, Tds1, Tds2>

template <typename T, typename Tds1, typename Tds2>
child_class<T,Tds1,Tds2>::
child_class(uint nclasses, double target_factor, bool binary_target_,
            t_confidence conf, bool apply_tanh_, const char *name_,
            int force, int single, idxdim *kerd, double sigma_scale,
                     bool silent)
    : parent_class<T, Tds1, Tds2>(nclasses, target_factor, binary_target_,
         conf, apply_tanh_, name_,
         force, single, kerd, sigma_scale, silent)
  {
      ...
  }

See this live example . 看到这个实例

As far as I can tell, you are simply missing a }; 据我所知,你只是错过了一个}; at the end of grandparent class. 在祖父母班级结束时。 When I added that (and tweaked the code to remove classes I don't have), it compiled fine in VS. 当我添加(并调整代码以删除我没有的类)时,它在VS中编译得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM