找回密码
 注册
Simdroid-非首页
查看: 102|回复: 4

请帮忙看一段混编程序吧

[复制链接]
发表于 2009-1-8 13:32:19 | 显示全部楼层 |阅读模式 来自 美国
最近初学混合编程,看董维国老师的《深入浅出Matlab混合编程》。
有一段C++写的Mex程序,编译通不过。找不出来原因,非常沮丧(说实话,我最怕就是把书上的码敲进去编译通不过了,感觉是自己犯了很低级的错误...)
大家帮忙看一下吧 谢谢了

#include "mex.h"
#include <vector>
#include <complex>

typedef std::vector<std::complex<double>> cmplxArray;

void mydiff(cmplxArray &z1, cmplxArray &z2, cmplxArray &out)
{
    // 清除输出向量
    out.clear();
   
    // 计算两个输入向量的差
    int i;
    for (i=0; i<z1.size(); ++i)
    {
        out.push_back(z1-z2);
    }
}

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    int i, m1, n1, m2, n2;
    double *datar1, *datai1, *datar2, *datai2;
    double *outr, *outi;
   
    // 输入变量个数必须为2
    if(nrhs != 2)
        mexErrMsgTxt("The number of inputs must be two.");
    // 输入必须是数字型
    for(i=0; i<nrhs; ++i)
    {
        if(mxIsChar(prhs)||mxIsClass(prhs,"sparse"))
            mexErrMsgTxt("Input must be real, full, and nonstring.");
    }
    // 得到每个输入矩阵的大小
    m1 = mxGetM(prhs[0]);
    n1 = mxGetN(prhs[0]);
   
    m2 = mxGetM(prhs[1]);
    n2 = mxGetN(prhs[1]);
   
    // 输入矩阵大小必须相同
    if(m1!=m2 || n1!=n2)
    {
        mexErrMsgTxt("The two inputs must be of the same size!");
    }
   
    // 创建输出变量所对应的mxArray
    plhs[0] = mxCreateDoubleMatrix(m1,n1,mxCOMPLEX);
   
    // 获得输入数据
    datar1 = mxGetPr(prhs[0]);
    datai1 = mxGetPi(prhs[0]);
    datar2 = mxGetPr(prhs[1]);
    datai2 = mxGetPi(prhs[1]);
   
    cmplxArray z1, z2, out;
   
    // 把数据存入向量中
    for(i=0; i<m1*n1; ++i)
    {
        z1.push_back(std::complex<double>(datar1,datai1));
        z2.push_back(std::complex<double>(datar2,datai2));
    }
   
    mydiff(z1, z2, out);
   
    // 输出变量的数据所对应数组
    outr = mxGetPr(plhs[0]);
    outi = mxGetPi(plhs[0]);
   
    // 把结果存入输出变量
    for(i=0; i<m1*n1; ++i)
    {
        outr = out.real();
        outi = out.imag();
    }
}
发表于 2009-1-8 15:07:29 | 显示全部楼层 来自 新加坡
Simdroid开发平台
What's the error message?

How did you compile the mex file (the complete command you ran in matlab)?

Which compiler you used when you "mex -setup"?

Which version of matlab are you using?
回复 不支持

使用道具 举报

 楼主| 发表于 2009-1-9 06:29:56 | 显示全部楼层 来自 美国
原帖由 taohe 于 2009-1-8 15:07 发表
What's the error message?
错误信息很多,所以没贴上来。我贴在下面吧。
How did you compile the mex file (the complete command you ran in matlab)?
mex diffComplex.cpp
Which compiler you used when you "mex -setup"?
VC 6.0
Which version of matlab are you using?
7.3.0(R2006b)



错误信息:
diffComplex.cpp
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\istream(547) : warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify -GX
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\complex(203) : warning C4275: non dll-interface class 'std::_Complex_base<float>' used as base for dll-interface class 'std::complex<float>'
        C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\complex(203) : see declaration of 'complex<float>'
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\complex(216) : warning C4275: non dll-interface class 'std::_Complex_base<double>' used as base for dll-interface class 'std::complex<double>'
        C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\complex(216) : see declaration of 'complex<double>'
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\complex(229) : warning C4275: non dll-interface class 'std::_Complex_base<long double>' used as base for dll-interface class 'std::complex<long double>'
        C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\complex(229) : see declaration of 'complex<long double>'
diffComplex.cpp(5) : error C2146: syntax error : missing ',' before identifier 'cmplxArray'
diffComplex.cpp(5) : error C2065: 'cmplxArray' : undeclared identifier
diffComplex.cpp(5) : error C2143: syntax error : missing '>' before ';'
diffComplex.cpp(5) : warning C4091: 'typedef ' : ignored on left of 'class std::vector' when no variable is declared
diffComplex.cpp(7) : error C2065: 'z1' : undeclared identifier
diffComplex.cpp(7) : error C2065: 'z2' : undeclared identifier
diffComplex.cpp(7) : error C2065: 'out' : undeclared identifier
diffComplex.cpp(8) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
diffComplex.cpp(49) : error C2146: syntax error : missing ';' before identifier 'z1'
diffComplex.cpp(53) : error C2228: left of '.push_back' must have class/struct/union type
diffComplex.cpp(54) : error C2228: left of '.push_back' must have class/struct/union type
diffComplex.cpp(57) : error C2065: 'mydiff' : undeclared identifier
diffComplex.cpp(64) : error C2109: subscript requires array or pointer type
diffComplex.cpp(64) : error C2228: left of '.real' must have class/struct/union type
diffComplex.cpp(65) : error C2109: subscript requires array or pointer type
diffComplex.cpp(65) : error C2228: left of '.imag' must have class/struct/union type

  D:\PROGRA~1\MATLAB~1\BIN\MEX.PL: Error: Compile of 'diffComplex.cpp' failed.
非常感谢!
回复 不支持

使用道具 举报

发表于 2009-1-10 10:56:36 | 显示全部楼层 来自 新加坡
原帖由 goso 于 2009-1-8 13:32 发表
最近初学混合编程,看董维国老师的《深入浅出Matlab混合编程》。
有一段C++写的Mex程序,编译通不过。找不出来原因,非常沮丧(说实话,我最怕就是把书上的码敲进去编译通不过了,感觉是自己犯了很低级的错误...)
大 ...


typedef std::vector<std::complex<double>> cmplxArray;

请注意看你的程序中这行中红色、粗体的那两个“>"号,C++语言中“>>"是一个有效的操作符,而在我们的程序中,应该用空格隔开那两个“>“括号。这在编写C++程序尤其是使用模板的时候需要特别注意。

附件是我用的例子。不好意思,回复地有点晚了。

[ 本帖最后由 taohe 于 2009-1-10 11:03 编辑 ]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
回复 不支持

使用道具 举报

 楼主| 发表于 2009-1-10 12:43:53 | 显示全部楼层 来自 美国
原帖由 taohe 于 2009-1-10 10:56 发表


typedef std::vector cmplxArray;

请注意看你的程序中这行中红色、粗体的那两个“>"号,C++语言中“>>"是一个有效的操作符,而在我们的程序中,应该用空格隔开那两个“>“括号。这在编写C++程序尤其是使用模板 ...

已经很及时了 您太客气了...  非常感谢!!
要不是您指出,这错误我怎么找也找不出来的.... :)  再次感谢...
回复 不支持

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Simapps系列直播

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-10-7 09:19 , Processed in 0.038989 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表