gpt4 book ai didi

c++ - 无法将参数 2 从 'mxArray **' 转换为 'mwArray &'

转载 作者:行者123 更新时间:2023-11-30 02:55:32 26 4
gpt4 key购买 nike

我想使用 deloytool 从 Matlab 创建一个 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 里面的代码如下:

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);
}

在构建项目时,出现如下错误:

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.

注意:我已经在 .cpp 源文件中包含了“fooCpplib.h”。

谁能帮我解决这个问题!谢谢!

最佳答案

当参数声明为

TypeName &argName

表示argName是一个引用。在 C++ 中,您可以通过引用传递参数,这允许函数修改您传递给它的变量(除其他外)。

如果你有一个指针,但一个函数需要一个引用,你需要在调用中用星号取消引用指针,而不是用 & 符号获取指针的地址:

foo(1,*y_ptr,*x_ptr);
// ^ ^
// | |
// Here and here

您可以根据间接级别 来考虑变量、指针、指向指针的指针等。变量的间接级别为零;指针的间接级别为 1;指向指针的指针的间接级别为 2,依此类推。

添加一个符号可以增加间接级别;添加星号会减少它。与变量一样,引用的间接级别为零。如果您有一个指针并且需要一个变量,则必须通过在前面加上星号来降低间接级别。

代码中的另一个问题是 foo 期望引用带有“w”的 mwArray,但您传递的是对 mxArray 的引用,带有“x”。类型需要匹配,否则编译器不会接受您的程序。

关于c++ - 无法将参数 2 从 'mxArray **' 转换为 'mwArray &',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16374140/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com