简体   繁体   English

将对象传递给另一个类的构造函数

[英]Pass object to constructor of another class

I have two objects, data which is a large amount of data (duh) and node which I plan to use in a tree, where each node contains data.我有两个目的, data是大量的数据(杜)和node ,我打算在一个树,其中每个节点包含数据使用。 I want to create a node constructor where I pass it a data object but I am getting the following error:我想创建一个node构造函数,在其中传递一个data对象,但出现以下错误:

node.cpp: In constructor 'node::node(data&, std::vector<int>)': node.cpp:7:49: error: no matching function for call to 'data::data()' node::node(data &data0, vector<int> feature_list)

My constructors for data are:我的data构造函数是:

 // "data.h"
 13     // read the data from a file
 14     data(string fname);
 15
 16     // data set based on other data set
 17     data(data &data0, int iv_beg, int iv_end);
 18
 19     // data set based on other data set
 20     data(data &data0, vector< int > vectors );

where the first one is such that I read the data from a file, the second two are methods for creating new data objects from previous data objects.第一个是我从文件中读取数据,后两个是从以前的data对象创建新data对象的方法。 The ability to pass data &data0 works fine in the data cosntructors, but when I try to create a node constructor:传递的能力, data &data0工作在精细data cosntructors,但是当我尝试创建一个node的构造函数:

 // "node.h"
 17     // create a node with the given data
 18     // --- tell it what features it is allowed to branch on
 19     node(data &data0, vector<int> feature_list);

I get the error.我得到了错误。 I know it is saying that I don't have a default constructor for data , but why do I need one?我知道这是说我没有data的默认构造函数,但为什么我需要一个? I am passing an object which has already been defined.我正在传递一个已经定义的对象。 Does data::data() , by default, allow for a copy or something that I'm not seeing?默认情况下, data::data()是否允许复制或我没有看到的东西? If so, why was I allowed to pass data &data0 to my data constructors without it barking at me?如果是这样,为什么允许我将data &data0传递给我的data构造函数而不data &data0我吠叫?

EDIT:编辑:

Simply placing data::data();简单地放置data::data(); into my data.h file and data::data() { } into my data.cpp file solves the problem, but I feel like there is something else going on - what am I missing about the way that objects get passed to constructors of other classes?进入我的data.h文件和data::data() { }进入我的data.cpp文件解决了这个问题,但我觉得还有其他事情发生 - 我错过了对象传递给构造函数的方式其他班级?

EDIT2: minimal code example. EDIT2:最小代码示例。

  "node.h":
  1 #ifndef _NODE_H_
  2 #define _NODE_H_
  3
  4 #include "data.h"
  5 #include <vector>
  6 #include <string>
  7
  8 class node {
  9   public:
 10     node(data &data0, vector<int> feature_list);
 11   private:
 12     data node_data; // data for this node
 13 };
 14 #endif

  "node.cpp"
  1 #include "node.h"
  2 #include "data.h"
  3 #include <vector>
  4
  5 node::node(data &data0, vector<int> feature_list) {
  6   data0.print();
  7 }

  "data.h"
  1 #ifndef _DATA_H_
  2 #define _DATA_H_
  3
  4 #include <string>
  5 #include <vector>
  6
  7 using namespace std;
  8
  9 /////// object to hold data
 10 class data {
 11
 12   public:
 13     // data set based on file
 14     data(string fname);
 15     // print the dat set
 16     void print();
 17     int nvectors();
 18     int nfeatures();
 19     vector< string > feature_names();
 20     vector< double > feature_vector(int iv);
 21
 22   private:
 23     void read_datfile();
 24     string _fname;
 25     vector< string > _features;
 26     vector<vector< double > > _x;
 27     int _Nf; // number of features
 28     int _Nv; // number of feature vectors
 29 };
 30
 31 #endif



  "data.cpp"
  1 #include "data.h"
  2
  3 #include <string>
  4 #include <string.h>
  5 #include <vector>
  6 #include <stdio.h>
  7 #include <stdlib.h>
  8 #include <fstream>
  9 #include <iostream>
 10
 11 using namespace std;
 12
 13 // data set which is derived fom a file
 14 data::data(string fname) {
 15   _fname = fname;
 16   read_datfile();
 17 }
 18
 19 // print the vectors in the data set
 20 void data::print() {
 21   int iv_beg = 0;
 22   int iv_end = 2;
 23
 24   vector<double> tvect[_Nf];
 25   if(iv_end < _Nv) {
 26     for(int iv = iv_beg; iv<iv_end; iv++) {
 27       printf("VECTOR %i\n", iv);
 28       for(int ifeat = 0; ifeat<_Nf; ifeat++) {
 29         printf("%25s ... %f\n", _features[ifeat].c_str(), _x[iv][ifeat]);
 30       }
 31     }
 32   }
 33   else {
 34     printf("ERROR: there are not that many vectors in this data set\n");
 35   }
 36 }
 37
 38 // read the file
 39 void data::read_datfile() {
 ...
 87 }

I'm pretty sure the problem is that you don't put it in the initialiser-list.我很确定问题是你没有把它放在初始化列表中。 Showing your code for node might help.显示您的node代码可能会有所帮助。 If you don't give your data member some value in the initialiser-list its default constructor will be called then you use the affectation operator to change its value which is probably what you don't want to do.如果你没有在初始化列表中给你的数据成员一些值,它的默认构造函数将被调用,那么你使用做作运算符来改变它的值,这可能是你不想做的。 If your class node contains a pointer instead of a reference it will be fine even if you don't initialise it because a pointer doesn't have to be initialised but a value or a reference has to (hence the call to the default constructor).如果您的类节点包含一个指针而不是一个引用,即使您不初始化它也没关系,因为不必初始化指针,但必须初始化值或引用(因此调用默认构造函数) .

What happens to node_data when the node constructor starts running?当节点构造函数开始运行时, node_data会发生什么? What is it going to be set to?它将被设置为什么? The compiler automatically attempts to call the default constructor.编译器会自动尝试调用默认构造函数。

This is another of an extremely long list of reasons why field initialization lists are ALWAYS best.这是为什么字段初始化列表总是最好的一个非常长的原因列表。 If you set node_data to data0 in the field initialization list, it will attempt to call your copy constructor instead of the default constructor (which it looks like you also don't have).如果设置node_datadata0在现场初始化列表,它会尝试打电话给你的拷贝构造函数,而不是默认的构造函数(它看起来像你还没有)。

If you don't want to define a copy constructor, then just stick with the solution you have right now and create a default constructor that does nothing.如果您不想定义复制构造函数,那么只需坚持使用您现在拥有的解决方案并创建一个不执行任何操作的默认构造函数。

Also, you're double including a couple times (this won't cause problems because the STL classes are protected from that, it's just pointless code).此外,您需要重复包含几次(这不会导致问题,因为 STL 类受到保护,这只是毫无意义的代码)。 If you include something in the .h file, it is already automatically included in the .cpp file when you do #include "foo.h"如果您在.h文件中包含某些内容,则在您执行#include "foo.h"时,它已自动包含在.cpp文件中

EDIT (field initialization list and copy constructor):编辑(字段初始化列表和复制构造函数):

class data {
    public:
        data(const data &that) { //Create the copy constructor
            //Simply copy over all of the values
            _fname = that._fname;
            _features = that._features;
            _x = that._x;
            _Nf = that._Nf;
            _Nv = that._Nv;
        }

        ...
}

node::node(data &data0, vector<int> feature_list) 
    : node_data(data0) { } //Initialize node_data in the initialization list

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

相关问题 将对象传递给类构造函数 - Pass an object to a class constructor 如何将“&player”对象传递给另一个继承的类构造函数(“ score”)? - How to pass “&player” object into another inherited class constructor (“score”)? 如何将一个对象作为另一个类构造函数的参数传递? - How to pass an object as another class constructor's parameter? 如何将我写入的对象传递给另一个类的构造函数? - How can I pass an object I wrote into a constructor of another class? 将父类传递给另一个类的构造函数 - Pass parent class to constructor of another class 无法将&#39;this&#39;的引用传递给另一个类的构造函数 - Unable to pass a reference of 'this' to a constructor of another class 将模板类的对象传递给另一个类的构造函数 - Passing object of a template class to constructor of another class Class 构造函数使用另一个 class 的 object - Class constructor using object of another class 具有另一个类对象属性的类的构造方法 - Constructor of a class with an attribute that is another class object 将类的实例传递给另一个构造函数,该构造函数将其对象添加到由传递的实例拥有的列表中 - Pass instance of class to a another constructor that adds its object to a list owned by passed instance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM