传递一个成员为vector的结构体

Pass a struct with a member being vector

本文关键字:成员 vector 结构体 一个      更新时间:2023-10-16

我有一个类,它构建一个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;
    };

在调用这个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;

现在一切看起来都很好。我可以看到param.winArray被正确地设置为正确的值。

然而,当我传递param作为参数时,param.winArray在容量/长度上变为0,正如我在dll中观察到的。

参数是这样传递的:

//caller.cpp
    ReconAxial16 operator;
    operator.Init( param ) ;

上面是参数传递到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));
        ......
    }

在这里,当我介入时,我可以看到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 ) ;

当然,在dll项目中,我做了这样的事情,使它接受数组作为附加参数:

// dll.h
LONG SetupGeometry( const ScanParam16 &param, float* wArray);   

它工作。我介入,看到wArray被正确地传递到dll中。