Nic*_*sui 5 c++ parameters struct vector parameter-passing
我有一个构建dll的类,作为单个解决方案实现.在它的头文件中,我有一个结构,其成员是vector.喜欢以下;
// dll.h
struct ScanParam16
{
// int param
int nChanDetX, nChanDetZ;
int nViewPerRot, nViewPerSlice,
nChanDetXPerMod;
int nImgXY, nImgZ;
int nSlicePerProcess, n2Group;
int FFTLen;
// float param
float pitch;
float isoOffX, isoOffZ;
float fov, dfov;
float imgCentX, imgCentY, imgCentZ;
float sdd, srad, drad;
float dDetX, dDetZ, dDetU, dDetV, interModGapX, dDetSampleRes;
std::vector<float> winArray;
bool interleave;
// enum
bpInterpType16 iType;
};
Run Code Online (Sandbox Code Playgroud)
在调用此dll的代码中,向量winArrar的值如下:
// caller.cpp
static ScanParam16 param;
param.FFTLen = 2048;
float* wArray = new float[param.FFTLen];
GenKernCoef(wArray, param.FFTLen, kType, ParaDataFloat, aram.dDetSampleRes);
std::vector<float> v(wArray, wArray+param.FFTLen);
param.winArray = v;
Run Code Online (Sandbox Code Playgroud)
现在一切都很好看.我可以看到param.winArray正确设置了正确的值.
但是,当我将param参数作为参数传递时,我param.winArray在dll中观察到的容量/长度变为0.
以下是param的传递方式:
//caller.cpp
ReconAxial16 operator;
operator.Init( param ) ;
Run Code Online (Sandbox Code Playgroud)
上面是参数传递给dll之前的点.
以下是参数进入dll的点:
// dll.cpp
void ReconAxial16::Init(const ScanParam16& param )
{
/**************************************************************/
// setup geometry and detv
/**************************************************************/
SetupGeometry(param);
// Allocate buffer for reconstructed image (on cpu side)
_img = (float *)malloc(_nImgXY * _nImgXY * sizeof(float));
......
}
Run Code Online (Sandbox Code Playgroud)
这里当我介入时,我可以看到param.winArray长度为0,但所有其他参数看起来都很好.
我不忍受它,并想知道如何正确传递矢量?非常感谢.
实际上我没有这个问题的答案,但我只是展示我如何通过绕过它来完成我想要的事情。
我基本上从结构中删除数组/向量,并将其作为第二个参数单独传递,如下所示:
//caller.cpp
ReconAxial16 operator;
float* wArray = new float[param.FFTLen];
GenKernCoef(wArray, param.FFTLen, kType, ParaDataFloat, param.dDetSampleRes);
operator.Init( param, wArray ) ;
Run Code Online (Sandbox Code Playgroud)
当然,在 dll 项目中,我做了类似的事情以使其接受数组作为附加参数:
// dll.h
LONG SetupGeometry( const ScanParam16 ¶m, float* wArray);
Run Code Online (Sandbox Code Playgroud)
有效。我介入,看到wArray正确地传递到了 dll 中。
| 归档时间: |
|
| 查看次数: |
84 次 |
| 最近记录: |