gpt4 book ai didi

c++ - 调用类的函数,但作为参数传递

转载 作者:行者123 更新时间:2023-11-28 05:46:32 24 4
gpt4 key购买 nike

我是 C++ 编程的新手(但更习惯 IDL 和 Python)。我实际上是在尝试将我已经用 IDL 编写的代码重写到 C++ 中,以提高其效率。在我的项目中,我希望从另一个类调用一个类中的函数。下面,我复制了我认为是理解问题所需的代码的重要部分。但是,我对其进行了简化以提高其清晰度。

用g++编译时,出现如下错误:

MALA.cpp: In member function ‘void MALA::update_position_MH(Model_def*, Data*, Config*, int)’:
MALA.cpp:277:52: error: no matching function for call to ‘Model_def::call_model(Data&)’
propos_model=(*model_class).call_model(*data_struc); // PROBLEM HERE! TO DEBUG!
^
In file included from MALA.h:13:0,
from MALA.cpp:29:
model_def.h:34:12: note: candidate: Eigen::VectorXd Model_def::call_model(Data*)
VectorXd call_model(Data *data_struc); // call a model using its name
^
model_def.h:34:12: note: no known conversion for argument 1 from ‘Data’ to ‘Data*’

我不明白的是,如果我注释该行(请参阅给定代码的末尾),代码编译时不会出错:

 propos_model=(*model_class).call_model(*data_struc); // PROBLEM HERE!

但是,为什么我也没有收到该行的错误:

vars_new=new_prop_values((*model_class).vars, m); // This works fine

将结构“Data”作为函数 call_model 的参数传递时我做错了什么?

这是简化的代码,散布在不同的文件中:

这是 model_def.h(简化版)

#include <Eigen/Dense>
#include <string>
#include "config.h"
#include "data.h"

class Model_def{
string model_name;
bool relax[];
int plength[];
VectorXd cons;
long Ncons, Nvars, Nparams;
string vars_names[];
string cons_names[];
string params_names[];
public:
Model_def(string m_name, bool rlx[], int plgth, VectorXd params_in, string params_in_names); // The constructor
Eigen::VectorXd params;
Eigen::VectorXd vars;
Eigen::VectorXd call_model(Data *data_struc);
};

这是 model_def.cpp(简化版)

#include <Eigen/Dense>
#include <iostream>
#include <iomanip>
#include "model_def.h"

Eigen::VectorXd Model_def::call_model(Data *data_struc){

bool passed=0;

if(model_name == "Model_MS_Global"){
passed=1;
return Model_MS_Global(params, plength, (*data_struc).x); // This function is in a dedicated cpp file (not shown here)
}

// Other things happening...
}

这是data.h

#include <Eigen/Dense>
#include <string>
struct Data{
Eigen::VectorXd x;
Eigen::VectorXd y;
long Nx; // Ny is not checked but should be as long as x
string xlabel[];
string ylabel[];
};

这是 MALA.h(简体)

#include <Eigen/Dense>
#include <string>
#include "model_def.h"

class MALA{
private:
int seed; // to generate random numbers
double epsilon1;
Eigen::MatrixXd epsilon2;
double A1;
double delta;
double delta_x;
public:
void update_position_MH(Model_def *model_class, Data *data_struc, Config *cfg_class, int m);
};

这是 MALA.cpp(简化版)

void MALA::update_position_MH(Model_def *model_class, Data *data_struc, Config *cfg_class, int m){

VectorXd vars_new;
double* u=uniform_01(1, &seed ); // Generate uniform random number between 0 and 1
VectorXd propos_model;

if((*cfg_class).proposal_type == "Random"){
vars_new=new_prop_values((*model_class).vars, m); // This works fine
}

propos_model=(*model_class).call_model(*data_struc); // PROBLEM HERE!

}

最佳答案

data_struc是一个指针,call_model要的是指针,直接传就可以了。将 * 放在前面“取消引用”它并使其不再是指针。

 propos_model=(*model_class).call_model(data_struc);

或者,更好一点

 propos_model = model_class->call_model(data_struc);

关于c++ - 调用类的函数,但作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36099085/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com