【发布时间】:2016-09-22 15:48:05
【问题描述】:
我的 C++ 类只有一种方法:
string MyThreadsCpp::MyThreadsCppClass::train(){
double sum = 0.0;
long max = 100 * 1000;
int perc = 0;;
for (long n = 0; n < max; n++){
sum += 4.0*pow(-1.0, n) / (2 * n + 1.0);
int rem = n % (max/10);
if (rem == 0){
perc = 100 * n / max;
cout << perc << "%" << endl;
}
}
cout << "100%" << endl;
ostringstream oss;
oss << "Result = " << sum;
return oss.str();
}
效果很好。
用于此的 C++/CLI 类库也只有一种方法:
string ThreadsCppWrapper::ThreadsCppWrapperClass::mytrainOp(int% i){
i++;
return ptr->train();
}
它构建得很好。
使用此 DLL 的 C# 代码:
namespace ThreadsCsharp
{
public partial class FrmMain : Form
{
private void btnTrain_Click(object sender, EventArgs e)
{
ThreadsCppWrapperClass obj = new ThreadsCppWrapperClass();
int i = 5;
obj.mytrainOp(i); /* This is where I get Error */
}
}
}
上述行的智能感知错误:
错误 1
方法“mytrainOp”没有重载需要 1 个参数
错误 2
指针和固定大小的缓冲区只能在不安全的上下文中使用
专家,请帮忙。
【问题讨论】:
-
用
ref调用它,即obj.mytrainOp(ref i); -
另外,您不能只从 C# 中读取
std::string,您必须返回System::String^,因此请返回msclr::interop::marshal_as<System::String^>(ptr->train())(需要#include <msclr/marshal_cppstd.h>) - 请参阅 @987654321 @. -
谢谢你,dbc abd Lucas
标签: c# c++ c++-cli mixed-mode