简体   繁体   English

Matlab的Mex文件使用向量类定义

[英]Matlab's Mex-file using a vector class definition

I am trying to create a Mex-file from a C++ code source file to use it with Matlab. 我正在尝试从C ++代码源文件创建一个Mex文件,以便与Matlab一起使用。 I have compiling errors due to the vector class definition handling that I do not understand very well. 由于我不太了解的向量类定义处理,我编译错误。 I would like to know how I should modify the code to work. 我想知道如何修改代码才能工作。 Below I show the relevant code's parts that I have divided into four sections for clarifying more (Computational code, MexFunction code, Vector class definition and compiling errors): 下面我将展示相关代码的各个部分,我将其分为四个部分以便进一步说明(计算代码,MexFunction代码,Vector类定义和编译错误):

Computational routine's code: 计算例程的代码:

#include "mex.h"
#include "SAT_VecMat.h"

void AccelSolrad (const Vector& r, const Vector& r_Sun, double Area, double mass,
    double CR, double P0, double AU,const Vector& Accel )

mexFunction's code: mexFunction的代码:

...
const Vector& r_sat(3);       // dummy argument name for r
const Vector& r_sol(3);       // dummy argument name for r_Sun      
const Vector& outMatrix(3);   // dummy argument name for Accel
...
r_sat = mxGetPr(prhs[0]);
r_sol = mxGetPr(prhs[1]);
plhs[0] = mxCreateDoubleMatrix(1,3,mxREAL);
outMatrix = mxGetPr(plhs[0]);

Vector class definition included in SAT_VecMat.h: SAT_VecMat.h中包含的向量类定义:

class Vector
{

  public:

    friend class Matrix;

    // Constructors    
    Vector ();                              // Vector without elements
    Vector (int Size);                      // Nullvector of specified size 
    Vector (const Vector& V);               // Vector copy
    Vector (const double* p, int N);        // Array copy
    Vector (double x, double y, double z);  // 3dim-Vector
    Vector (double x, double y, double z,   // 6dim-Vector
            double X, double Y, double Z);  

    // Destructor
    ~Vector();

    // Size
    int size() const { return n; };
    Vector& resize(int Size);

    // Assignment
    Vector& operator=(const double value);
    Vector& operator=(const Vector& V);

    // Component access (Fortran notation)
    double  operator () (int i) const { return v[i]; };
    double& operator () (int i)       { return v[i]; };
    Vector slice (int first, int last) const;
...

Compiling errors: 编译错误:

    >> mex AccelSolrad.cpp

    AccelSolrad.cpp 

    c:\program files\matlab\r2009b\extern\include\matrix.h(332) : error C2371: 'char16_t' : redefinition; different basic types

    C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\yvals.h(576) : see declaration of 'char16_t' 

AccelSolrad.cpp(14) : error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const Vector' (or there is no acceptable conversion) 
        d:\po\ejemplos\SAT_VecMat.h(69): could be 'Vector &Vector::operator =(const double)' 
        d:\po\ejemplos\SAT_VecMat.h(70): or       'Vector &Vector::operator =(const Vector &)' 
        while trying to match the argument list '(const Vector, Vector)' 

AccelSolrad.cpp(18) : error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const Vector' (or there is no acceptable conversion) 
        d:\po\ejemplos\SAT_VecMat.h(69): could be 'Vector &Vector::operator =(const double)' 
        d:\po\ejemplos\SAT_VecMat.h(70): or       'Vector &Vector::operator =(const Vector &)' 
        while trying to match the argument list '(const Vector, Vector)' 

AccelSolrad.cpp(94) : error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const Vector' (or there is no acceptable conversion) 
        d:\po\ejemplos\SAT_VecMat.h(69): could be 'Vector &Vector::operator =(const double)' 
        d:\po\ejemplos\SAT_VecMat.h(70): or       'Vector &Vector::operator =(const Vector &)' 
        while trying to match the argument list '(const Vector, double *)' 

AccelSolrad.cpp(96) : error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const Vector' (or there is no acceptable conversion) 
        d:\po\ejemplos\SAT_VecMat.h(69): could be 'Vector &Vector::operator =(const double)' 
        d:\po\ejemplos\SAT_VecMat.h(70): or       'Vector &Vector::operator =(const Vector &)' 
        while trying to match the argument list '(const Vector, double *)' 

AccelSolrad.cpp(112) : error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const Vector' (or there is no acceptable conversion) 
        d:\po\ejemplos\SAT_VecMat.h(69): could be 'Vector &Vector::operator =(const double)' 
        d:\po\ejemplos\SAT_VecMat.h(70): or       'Vector &Vector::operator =(const Vector &)' 
        while trying to match the argument list '(const Vector, double *)' 

  C:\PROGRA~1\MATLAB\R2009B\BIN\MEX.PL: Error: Compile of 'AccelSolrad.cpp' failed. 

Update: 更新:

At least the error message about char16_t is likely due to the fact that you are using Visual Studio 2010 with MATLAB R2009b. 至少有关char16_t的错误消息可能是由于您使用Visual Studio 2010和MATLAB R2009b这一事实。 That compiler is too new for this version of MATLAB. 对于这个版本的MATLAB,该编译器太新了。 See the Supported Compilers list for R2009b . 请参阅R2009b的“ 支持的编译器”列表

Not too clear about the other errors, but: 关于其他错误不太清楚,但是:

 Vector& r_sat(3);

Seems to me to be pretty questionable C++: Since it is a reference to a vector, what you are doing is creating a temporary Vector with size 3, then initializing the reference to refer to that temporary. 对我来说似乎很有问题C ++:因为它是对向量的引用,你正在做的是创建一个大小为3的临时Vector,然后初始化引用以引用那个临时的。 Better to just declare r_sat as Vector . 最好将r_sat声明为Vector

Then later you have: 然后你有:

r_sat = mxGetPr(prhs[0]);

The mxGetPr() function returns a pointer to a double array, but the Vector class does not have an operator= that takes a double* as an argument. mxGetPr()函数返回一个指向double数组的指针,但Vector类没有operator=double*作为参数。

Maybe something like this: 也许是这样的:

r_sat = Vector(mxGetPr(prhs[0]), 3); // use the Vector(double*, int N) constructor

Previous answer: 上一个答案:

  1. Your Vector class is C++, not C. Vector类是C ++,而不是C.
  2. Since you are on Windows, the lcc compiler that ships with MATLAB is a C compiler, it does not understand C++. 由于您使用的是Windows,因此MATLAB附带的lcc编译器是C编译器,它不了解C ++。 If you are going to be compiling C++ MEX files, you will need to run mex -setup and pick a supported C++ compiler (typically, Visual Studio). 如果您要编译C ++ MEX文件,则需要运行mex -setup并选择支持的C ++编译器(通常是Visual Studio)。
  3. The way mex decides whether you're building a C or C++ MEX file (which influences whether it invokes the C or C++ compiler to do the actual compiling and linking) is based on the file extension. mex决定是否构建C或C ++ MEX文件(影响它是否调用C或C ++编译器来进行实际编译和链接)的方式基于文件扩展名。 So rename AccelSolrad.c to AccelSolrad.cpp and you should be good to go. 所以将AccelSolrad.c重命名为AccelSolrad.cpp ,你应该好好去。

I think I might have something....though I've never worked with const obj& before. 我想我可能会有一些东西....虽然我以前从未使用过const obj&

You're instancing the vector class in your main code as a constant: 您将主代码中的向量类实例化为常量:

const Vector& r_sat(3);

Then you're trying to assign to the vector r_span an array. 然后你试图将矢量r_span分配给一个数组。 Since r_span was declared constant you can't. 由于r_span被声明为常量,你不能。 You have to do that as part of the constructor. 您必须将其作为构造函数的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM