[英]Pass object to constructor of another class
我有兩個目的, data
是大量的數據(杜)和node
,我打算在一個樹,其中每個節點包含數據使用。 我想創建一個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)
我的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 );
第一個是我從文件中讀取數據,后兩個是從以前的data
對象創建新data
對象的方法。 傳遞的能力, 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);
我得到了錯誤。 我知道這是說我沒有data
的默認構造函數,但為什么我需要一個? 我正在傳遞一個已經定義的對象。 默認情況下, data::data()
是否允許復制或我沒有看到的東西? 如果是這樣,為什么允許我將data &data0
傳遞給我的data
構造函數而不data &data0
我吠叫?
編輯:
簡單地放置data::data();
進入我的data.h
文件和data::data() { }
進入我的data.cpp
文件解決了這個問題,但我覺得還有其他事情發生 - 我錯過了對象傳遞給構造函數的方式其他班級?
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 }
我很確定問題是你沒有把它放在初始化列表中。 顯示您的node
代碼可能會有所幫助。 如果你沒有在初始化列表中給你的數據成員一些值,它的默認構造函數將被調用,那么你使用做作運算符來改變它的值,這可能是你不想做的。 如果您的類節點包含一個指針而不是一個引用,即使您不初始化它也沒關系,因為不必初始化指針,但必須初始化值或引用(因此調用默認構造函數) .
當節點構造函數開始運行時, node_data
會發生什么? 它將被設置為什么? 編譯器會自動嘗試調用默認構造函數。
這是為什么字段初始化列表總是最好的一個非常長的原因列表。 如果設置node_data
到data0
在現場初始化列表,它會嘗試打電話給你的拷貝構造函數,而不是默認的構造函數(它看起來像你還沒有)。
如果您不想定義復制構造函數,那么只需堅持使用您現在擁有的解決方案並創建一個不執行任何操作的默認構造函數。
此外,您需要重復包含幾次(這不會導致問題,因為 STL 類受到保護,這只是毫無意義的代碼)。 如果您在.h
文件中包含某些內容,則在您執行#include "foo.h"
時,它已自動包含在.cpp
文件中
編輯(字段初始化列表和復制構造函數):
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.