C++ 将void指针和结构传递给函数-c++;
我有一个函数,它将指向void的指针作为参数。所以我给这个函数传递了一个struct,但在这个函数中我调用了另一个函数(f_pD),它将一个结构(struct)作为参数。 在函数f_mD1内部,我重置了我的结构,然后调用f_pD函数,该函数以一个struct作为参数。我不想在主函数中完成结构重置后再重置结构。有人能帮我吗?下面是代码的草图。C++ 将void指针和结构传递给函数-c++;,c++,C++,我有一个函数,它将指向void的指针作为参数。所以我给这个函数传递了一个struct,但在这个函数中我调用了另一个函数(f_pD),它将一个结构(struct)作为参数。 在函数f_mD1内部,我重置了我的结构,然后调用f_pD函数,该函数以一个struct作为参数。我不想在主函数中完成结构重置后再重置结构。有人能帮我吗?下面是代码的草图。在此处输入代码 #include <fstream> #include <map> using namesp
在此处输入代码
#include <fstream>
#include <map>
using namespace std;
struct pr_pD {
int last; double xD; map <double, double> tab_w;
} geral;
double f_pD (double, double, double, double, struct pr_pD geral);
double f_mD1 (double, double, double, double, struct pr_pD geral);
static int Integrand_pD(const int *ndim, const double k[], const int *ncomp, double FG[], void *parametros)
{
struct pr_pD * fp = (struct pr_pD *)parametros;
FG[0] = fp->last*fp->xD;
}
static int Integrand_mD1(const int *ndim, const double k[], const int *ncomp, double F[], void *parametros)
{
struct pr_pD * fp = (struct pr_pD *)parametros;
// -------------------------------------------
// I want to eliminate this part of the code
struct pr_pD geral;
geral.tab_w = fp->tab_w;
geral.last = fp->last;
geral.xD = fp->xD;
//----------------------------------------------
double pD;
pD = f_pD( fp->xD*k[1], fp->last*k[2], k[3], k[0], geral );
F[0] = 2*pD;
}
int main()
{
tab_w.insert(pair<double, double>( 1 , 2 ) );
geral.tab_w = tab_w;
geral.last = 1;
geral.xD = 2.5;
double mD1
mD1 = f_mD1(1, 1, 1, 0.1, geral );
return 0;
}
double f_pD(double xD, double yD, double zD, double tD, struct pr_pD geral)
{
double val_int;
val_int = Cuhre(Integrand_pD, xD, yD, zD, tD);
return geral.last*val_int;
}
double f_mD1(double xD, double yD, double zD, double tD, struct pr_pD geral)
{
double val_int;
val_int=Vegas(Integrnd_mD1, xD, yD, zD, tD);
return geral.last*val_int;
}
#包括
#包括
使用名称空间std;
结构公关部{
int last;双xD;映射选项卡w;
}杰拉尔;
双f_pD(双,双,双,双,结构P_pD geral);
双f_mD1(双,双,双,双,双,结构PRU pD geral);
静态整型被积函数(常数整型*ndim,常数双k[],常数整型*ncomp,双FG[],void*参数)
{
结构pr_pD*fp=(结构pr_pD*)参数;
FG[0]=fp->last*fp->xD;
}
静态整型被积函数(常数整型*ndim,常数双k[],常数整型*ncomp,双F[],void*参数)
{
结构pr_pD*fp=(结构pr_pD*)参数;
// -------------------------------------------
//我想删除这部分代码
结构件;
geral.tab\u w=fp->tab\u w;
geral.last=fp->last;
geral.xD=fp->xD;
//----------------------------------------------
双pD;
pD=f_-pD(fp->xD*k[1],fp->last*k[2],k[3],k[0],杰拉尔);
F[0]=2*pD;
}
int main()
{
选项卡w.插入件(对(1,2));
geral.tab_w=tab_w;
杰拉尔·拉斯特=1;
geral.xD=2.5;
双mD1
mD1=f_mD1(1,1,1,0.1,杰拉尔);
返回0;
}
双f_pD(双xD、双yD、双zD、双tD、结构P_pD geral)
{
双重价值;
val_int=Cuhre(被积函数_pD,xD,yD,zD,tD);
返回最后一个总日数*值;
}
双f_mD1(双xD、双yD、双zD、双tD、结构pr_pD geral)
{
双重价值;
val_int=Vegas(积分mD1、xD、yD、zD、tD);
返回最后一个总日数*值;
}
您可以使用*fp而不是geral参数,只需省略您想要删除的代码即可
pD = f_pD( fp->xD*k[1], fp->last*k[2], k[3], k[0], *fp );
顺便说一下,这些函数不修改其geral参数,因此可以通过引用接受该参数,从而避免潜在的昂贵副本:
double f_pD(double xD, double yD, double zD, double tD, const pr_pD& geral)