【发布时间】:2014-07-23 18:57:12
【问题描述】:
我有一个函数
float * pointwise_search(vector<float > &P,vector<float > &Q,float* n, int len ).
我想让 matlab 调用它,所以我需要编写一个 mexFunction。
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
if (nrhs != 4)
{
mexErrMsgTxt("Input is wrong!");
}
float *n = (float*) mxGetData(prhs[2]);
int len = (int) mxGetScalar(prhs[3]);
vector<float > Numbers= (vector<float >)mxGetPr(prhs[0]);
vector<float > Q= (vector<float >)mxGetPr(prhs[1]);
plhs[1] = pointwise_search(Numbers,Q,n,len );
}
但我发现vector<float > Numbers= (vector<float >)mxGetPr(prhs[0]);
vector<float > Q= (vector<float >)mxGetPr(prhs[1]); 是错误的。
所以我必须将float * pointwise_search(vector<float > &P,vector<float > &Q,float* n, int len ) 更改为float * pointwise_search(float *P,float *Q,float* n, int len )。
根据答案,我改写如下
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();
...
}
但是会出现错误。
pointwise_search.cpp(12) : error C2601: 'P' : local function definitions are illegal
pointwise_search.cpp(11): this line contains a '{' which has not yet been matched
作为评论,我应该将vector<float> P{p, p + num_thres}; 更改为vector<float> P(p, p + num_thres);。 :)
【问题讨论】:
-
使用向量的迭代器对构造函数和数据的开始和结束指针。