c++ - cannot convert parameter 2 from 'mxArray **' to 'mwArray &' -
i want create c++ shared library matlab using deloytool , use in mvs. compile function name 'foo.m', result got list of files (.h,.cpp,.lib,...) , found function in 'foocpplib.h' follow:
extern lib_foocpplib_cpp_api void mw_call_conv foo(int nargout, mwarray& y, const mwarray& x);
then create mvs project (2010), window form application, 2 textbox , 2 click button, 1 textbox named inbox, , named outbox. code inside button_click 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 mxarray input mlffoo x_ptr = mxcreatedoublescalar(input); // call implementation function // note second input argument should &y_ptr instead of y_ptr. foo(1,&y_ptr,x_ptr); // return value mlffoo mxarray. // use mxgetpr pointer data contains. y = (double*)mxgetpr(y_ptr); // display result in form outbox->text = ""+*y; //clean memory mxdestroyarray(x_ptr); mxdestroyarray(y_ptr); }
when build project, error occurred below:
error c2664: 'foo' : cannot convert parameter 2 'mxarray **' 'mwarray &' types pointed unrelated; conversion requires reinterpret_cast, c-style cast or function-style cast.
note: included 'foocpplib.h' in .cpp source file.
could me this! thank you!
when parameter declared as
typename &argname
it means argname
reference. in c++ can pass parameters reference, allows function modify variable pass (among other things).
if have pointer, function expects reference, need dereference pointer in call asterisk, rather taking address of pointer ampersand:
foo(1,*y_ptr,*x_ptr); // ^ ^ // | | // here , here
you can think of variables, pointers, pointers pointers, etc. in terms of level of indirection. variables have level of indirection of zero; pointers have level of indirection of one; pointers pointers have level of indirection of two, , on.
adding ampersand increases level of indirection; adding asterisk decreases it. variables, references have level of indirection of zero. if have pointer , need variable, must decrease level of indirection prepending asterisk.
another problem in code foo
expects references mwarray
, "w", passing references mxarray
, "x". types need match, otherwise compiler not going take program.
Comments
Post a Comment