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

3
我将尝试从C++代码源文件中创建一个Mex文件,以便在Matlab中使用。由于我不太理解向量类定义处理的编译错误,所以出现了编译错误。我想知道如何修改代码使其正常工作。下面我将展示相关代码部分,我将其分为四个部分以更清晰地说明(计算代码、MexFunction代码、向量类定义和编译错误):

计算例程代码:

#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的代码:

...
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]);

SAT_VecMat.h 中包含的 Vector 类定义:

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;
...

编译错误:

    >> 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. 

@0A0D:问题是如何修改AccelSolrad(特别是我认为使用Vector类定义和操作r_sat、r、r_Sun和r_sol向量的代码行)。r_Sun - julianfperez
@Amro:感谢您的编辑。干杯。 - julianfperez
2个回答

6

更新:

至少关于char16_t的错误信息可能是由于您正在使用Visual Studio 2010与MATLAB R2009b。这个编译器对于这个版本的MATLAB来说太新了。请参阅R2009b支持的编译器列表

其他错误不是很清楚,但是:

 Vector& r_sat(3);

我认为这段 C++ 代码有些问题:由于它是对向量的引用,你所做的是创建一个大小为 3 的临时向量,然后初始化引用以指向该临时向量。更好的做法是将 r_sat 声明为 Vector

然后稍后你会看到:

r_sat = mxGetPr(prhs[0]);
< p > mxGetPr()函数返回一个double数组的指针,但是Vector类没有以double*作为参数的operator=

可以尝试使用以下方法:

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

之前的回答:

  1. 你的 Vector 类是 C++,不是 C。
  2. 由于你在 Windows 上,MATLAB 附带的 lcc 编译器是 C 编译器,它不理解 C++。如果你要编译 C++ MEX 文件,你需要运行 mex -setup 并选择一个支持的 C++ 编译器(通常是 Visual Studio)。
  3. mex 决定你正在构建 C 或 C++ MEX 文件的方式(这影响它是否调用 C 或 C++ 编译器进行实际的编译和链接)是基于文件扩展名的。所以将 AccelSolrad.c 重命名为 AccelSolrad.cpp,然后你应该就可以了。

非常感谢您的回答。您是正确的。源代码文件确实是C++,我正在使用Visual Studio作为编译器。干杯。 - julianfperez

1

我觉得我可能有点头绪……虽然我以前从未使用过const obj&

你在主代码中将向量类实例化为常量:

const Vector& r_sat(3);

然后你试图将一个数组分配给向量r_span。由于r_span被声明为常量,所以你不能这样做。你必须在构造函数的一部分中完成它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接