Matlab的Mex文件使用矢量类定义
|
我正在尝试从C ++代码源文件创建Mex文件,以将其与Matlab结合使用。由于我不太了解矢量类定义的处理方式,因此出现编译错误。我想知道如何修改代码才能正常工作。下面,我显示了相关代码的部分,我将其分为四个部分以进行更多说明(计算代码,MexFunction代码,Vector类定义和编译错误):
计算例程的代码:
#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中包含的向量类定义:
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.
没有找到相关结果
已邀请:
2 个回复
暑袜眠退
在我看来,C ++相当可疑:由于它是对vector的引用,因此您正在做的工作是创建一个大小为3的临时Vector,然后初始化引用以引用该临时对象。 最好只是将
声明为
。 然后,您将拥有:
函数返回指向双精度数组的指针,但是Vector类没有以
作为参数的
。 也许是这样的:
先前的答案: 您的“ 7”课是C ++,而不是C。 由于您使用的是Windows,因此MATLAB附带的
编译器是C编译器,它不了解C ++。如果要编译C ++ MEX文件,则需要运行
并选择一个受支持的C ++编译器(通常是Visual Studio)。
决定您要构建C还是C ++ MEX文件的方式(会影响它是否调用C或C ++编译器进行实际的编译和链接)是基于文件扩展名的。因此,将
重命名为
,您应该就可以了。
系漏
然后,您尝试为向量r_span分配一个数组。由于r_span被声明为常量,所以不能。您必须在构造函数中执行此操作。