不能将参数2从'mxArray **'转换为'mwArray&' [英] cannot convert parameter 2 from 'mxArray **' to 'mwArray &'
问题描述
我想从Matlab使用deloytool创建一个C ++共享库,并在MVS中使用它。我编译一个函数名'foo.m',结果我得到文件列表(.h,.cpp,.lib,...),我发现'fooCpplib.h'中的函数如下:
extern LIB_fooCpplib_CPP_API void MW_CALL_CONV foo(int nargout,mwArray& y,const mwArray& x);然后我创建了一个MVS项目(2010),窗体窗体应用程序,有2个文本框和2个点击按钮,一个名为inBox的文本框,另一个名为outBox的文本框。 button_click中的代码如下:
是一个引用。在C ++中你可以通过引用传递参数,这允许函数修改你传递给它的变量(除了别的以外)。private:System :: Void button1_Click(System :: Object ^ sender,System :: EventArgs ^ e){
double input = System :: Double :: Parse(inBox-> Text);
mxArray * x_ptr;
mxArray * y_ptr = NULL;
double * y;
//创建一个mxArray以输入到mlfFoo
x_ptr = mxCreateDoubleScalar(input);
//调用实现函数
//注意第二个输入参数应该是& y_ptr而不是y_ptr。
foo(1,& y_ptr,x_ptr);
// mlfFoo的返回值是一个mxArray。
//使用mxGetpr获取它包含的数据的指针。
y =(double *)mxGetPr(y_ptr);
//以
outBox-> Text =+ * y的形式显示结果;
//清理内存
mxDestroyArray(x_ptr);
mxDestroyArray(y_ptr);
}
当我构建项目时,发生错误如下:
错误C2664:'foo':无法将参数2从'mxArray **'转换为'mwArray&'
是不相关的;转换需要reinterpret_cast,C风格的转换或函数式
转换。
注意:我已经在.cpp源文件中包含了'fooCpplib.h'。
任何人都可以帮助我这个!
谢谢!解决方案当参数声明为
TypeName& argName
c $ c> argName
如果你有一个指针,函数需要引用,您需要使用星号解除引用指针,而不是使用带&号的指针的地址:
foo(1,* y_ptr,* x_ptr);
// ^ ^
// | |
//这里和这里
你可以想到变量,指针,指针指针,等等,根据间接级别。变量具有零间接水平;指针具有一个间接水平;
添加一个&符号增加了间接的水平;添加星号会减少它。像变量一样,引用的间接级别为零。如果你有一个指针,并且你需要一个变量,你必须通过添加一个星号来减少间接的水平。
你的代码中的另一个问题是
foo
期望引用mwArray
,带有一个w,但是你引用mxArray
,带有x。类型需要匹配,否则编译器不会占用你的程序。I want to create a C++ Shared library from Matlab using deloytool and use it in MVS. I compile a function name 'foo.m', as the result I got list of files (.h,.cpp,.lib,...) and I found the function in 'fooCpplib.h' is as follow:
extern LIB_fooCpplib_CPP_API void MW_CALL_CONV foo(int nargout, mwArray& y, const mwArray& x);
Then I create a MVS project (2010), window form application, with 2 textbox and 2 click button, one textbox named inBox, and another named outBox. The code inside button_click is as follow:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { double input = System::Double::Parse(inBox->Text); mxArray *x_ptr; mxArray *y_ptr=NULL; double *y; // Create an mxArray to input into mlfFoo x_ptr = mxCreateDoubleScalar(input); // Call the implementation function // Note the second input argument should be &y_ptr instead of y_ptr. foo(1,&y_ptr,x_ptr); // The return value from mlfFoo is an mxArray. // Use mxGetpr to get a pointer to data it contains. y = (double*)mxGetPr(y_ptr); // display the result in the form outBox->Text = ""+*y; //clean up memory mxDestroyArray(x_ptr); mxDestroyArray(y_ptr); }
When I build the project, the error was occurred as below:
error C2664: 'foo' : cannot convert parameter 2 from 'mxArray **' to 'mwArray &' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast.
Note: I already included 'fooCpplib.h' in the .cpp source file.
Could anyone help me about this! Thank you!
解决方案When a parameter is declared as
TypeName &argName
it means that
argName
is a reference. In C++ you can pass parameters by reference, which allows the function modify the variable that you pass to it (among other things).If you have a pointer, but a function expects a reference, you need to dereference the pointer in the call with an asterisk, rather than taking the address of a pointer with an ampersand:
foo(1,*y_ptr,*x_ptr); // ^ ^ // | | // Here and here
You can think of variables, pointers, pointers to pointers, etc. in terms of level of indirection. Variables have a level of indirection of zero; pointers have level of indirection of one; pointers to pointers have level of indirection of two, and so on.
Adding an ampersand increases the level of indirection; adding an asterisk decreases it. Like variables, references have level of indirection of zero. If you have a pointer and you need a variable, you must decrease the level of indirection by prepending an asterisk.
Another problem in your code is that
foo
expects references tomwArray
, with a "w", but you are passing references tomxArray
, with an "x". The types need to match, otherwise the compiler is not going to take your program.这篇关于不能将参数2从'mxArray **'转换为'mwArray&'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!