c++ - 传递成员为 vector 的结构

标签 c++ vector struct parameters 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;
    };

在调用此 dll 的代码中, vector 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,但所有其他参数看起来都很好。

我不明白,想知道如何才能正确传递 vector ?非常感谢。

最佳答案

我实际上没有这个问题的答案,但我只是展示我是如何通过绕过它来做我想做的。

我基本上从结构中剥离了数组/vector ,并将其作为第二个参数单独传递,如下所示:

 //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 中。

关于c++ - 传递成员为 vector 的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38882256/

相关文章:

c++ - 链表离开递归函数后丢失节点

c++ - 比较两个 vector 元素并得到 EXC_BAD_ACCESS(code=1, address = 0x0) 错误

c - 如何使用结构体

c - 如何在数组中使用int?

.net - 为什么 F# inline 会导致 11 倍的性能提升

c++ - Lua 变量作用域、函数前向声明和使用 'require' 语句重构 Lua 脚本

c++ - 手动停止 QDrag

arrays - 两个数组 LISP 的两个值之和

c++ - 从 C++ vector 中提取引用

c++ - 子 vector 修改原始 vector