c++ /CLI:不能转换参数
本文关键字:转换 参数 不能 CLI c++ | 更新日期: 2023-09-27 18:13:12
我有一个小问题,我先解释一下。我正试图将c#代码与c# dll一起转换为使用c++/CLI的c++,因此我的c++应用程序可以与c# dll一起工作。下面是c#代码的一部分
private void USB_OnSpecifiedDeviceRemoved(object sender, EventArgs e)
{
this.DevicePresent = false;
}
this.USB.OnSpecifiedDeviceRemoved += new EventHandler(this.USB_OnSpecifiedDeviceRemoved);
下面是我的c++转换
usb.OnSpecifiedDeviceRemoved += System::EventHandler(this->USB_OnSpecifiedDeviceRemoved(nullptr,nullptr));
void MissileLauncher::USB_OnSpecifiedDeviceRemoved(System::Object sender, System::EventArgs e)
{
}
当我运行c++代码时,我得到以下错误
1>------ Build started: Project: CallToCSharp, Configuration: Debug Win32 ------
1> MissileLauncher.cpp
1>MissileLauncher.cpp(109): error C2664: 'MissileLauncher::USB_OnSpecifiedDeviceRemoved' : cannot convert parameter 1 from 'nullptr' to 'System::Object'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>MissileLauncher.cpp(109): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
为什么会这样?什么好主意吗?
我看到你的转换有两个问题。首先,您错误地将事件处理程序添加到事件中。它应该像下面这样:
usb.OnSpecifiedDeviceRemoved +=
gcnew System::EventHandler(this,
&MissileLauncher::USB_OnSpecifiedDeviceRemoved);
其次,事件处理程序的签名不正确。您需要对参数使用跟踪引用,这些参数使用^
:
void MissileLauncher::USB_OnSpecifiedDeviceRemoved(System::Object ^sender,
System::EventArgs ^e)
希望对你有帮助。