【问题标题】:An error cannot convert from 'void *' to 'float *' `错误无法从 'void *' 转换为 'float *' `
【发布时间】:2014-07-23 20:46:38
【问题描述】:

我编写了一个 c++ 函数及其关联的 mex。但是c++函数的一种输入是double *

  1. 函数pointwise_search的输出是一个指针。我被告知我应该删除它。但我不知道应该在哪里删除它,因为我需要它作为输出。

  2. 从答案中,我知道我应该检查mxIsSingle 的输入类型。所以我更正了函数mexFunction。但是有一个错误error C2440: '=' : cannot convert from 'void *' to 'float *'

  3. 在 matlab 中,我应该调用 pointwise_search(float *p,float q, num_thres,float n, len )。如果我在 matlab 中有一个向量 v_in_matlab=rand(5,1) 。我应该通过p=single(v_in_matlab);pointwise_search(p... 得到它的指针

提前致谢。

#include "mex.h"
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>

using namespace std;


float * pointwise_search(float *p,float *q,int num_thres, float* n, int len )
{
    vector<float> P(p, p + num_thres);
    vector<float> Q(q, q + num_thres);
    int size_of_threshold = P.size();
    float  *Y=new float[len];
    float *z=new float[len];
    typedef vector<float > ::iterator IntVectorIt ;
    IntVectorIt start, end, it, location ;
    start = P.begin() ;   // location of first
    // element of Numbers

    end = P.end() ;       // one past the location
    // last element of Numbers

    for (int i=0;i<len;i++)
    {
        location=lower_bound(start, end, n[i]) ;
        z[i]=location - start;
        if(z[i]>0&&z[i]<size_of_threshold)
        {

            Y[i]=(n[i]-P[z[i]])/(P[z[i]-1]-P[z[i]])*(Q[z[i]-1]-Q[z[i]])+Q[z[i]];
        }
        else
        {
            Y[i]=Q[z[i]];
        }
    }

    return (&Y[0]);
}




 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
   {
    float * Numbers, *Q;
    if (nrhs != 5)
    {
        mexErrMsgTxt("Input is wrong!");
    }
    float *n = (float*) mxGetData(prhs[3]);
    int len = (int) mxGetScalar(prhs[4]);
    int num_thres = (int) mxGetScalar(prhs[2]);

    /* Input gs */

    if(mxIsComplex(prhs[0])
    ||!mxIsSingle(prhs[0]))
        mexErrMsgTxt("Input 0 should be a class Single");
    /* get the pointer to gs */
    Numbers=mxGetData(prhs[0]);


    if(mxIsComplex(prhs[0])
    ||!mxIsSingle(prhs[0]))
        mexErrMsgTxt("Input 0 should be a class Single");
    /* get the pointer to gs */
    Q=mxGetData(prhs[1]);

//     float * Numbers= (float *)mxGetData(prhs[0]);
//     float * Q= (float *)mxGetData(prhs[1]);

    float * out= pointwise_search(Numbers,Q,num_thres,n,len );
    //float* resizedDims = (float*)mxGetPr(out);
}

【问题讨论】:

    标签: c++ matlab mex


    【解决方案1】:

    在 Matlab 中使用 single() 在调用 mexFunction 之前转换数据。在 C++ 端,通过mxIsSingle() 验证类型确实是单一的。在此之后,您可以愉快地投到float*

    【讨论】:

      【解决方案2】:

      在您担心 MEX 代码之前,请先再看看您的 C++ 函数。你有一些非常明显的memory leaksnew 但没有delete[])。

      关于 MEX,你不应该看到这个:

      (float *)mxGetPr(prhs[0])
      

      您不能将 double* 转换为 float* 并期望这些数字有意义。从 MATLAB 输入single 并使用:

      (float *)mxGetData(prhs[0])
      

      按照 Trilarion 的建议进行操作,并针对预期的数据类型测试您的所有输入 mxArrays。

      【讨论】:

      • 感谢您的评论。你能告诉我应该在哪里删除函数pointwise_search的输出pointwise_search
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-23
      • 1970-01-01
      • 2018-02-26
      相关资源
      最近更新 更多