1

我正在尝试使用 c++/cli 代码从 ac# 调用非托管函数。

我有类似这样的代码:

MyFileWrapper MyFileWrapper::ReadMyFile(System::String ^fileName)
{
    auto nativeMyFile=MyFile::ReadMyFile((char *) 
           System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(
           fileName).ToPointer());
    MyFileWrapper myFileWrapper=gcnew MyFileWrapper();
     // code will be added to read information from native object and convert them to wrapper properties. 
    return myFileWrapper;

}

但我收到错误

 Error  2   error C2440: 'return' : cannot convert from 'MyFileWrapper' to 'MyFileWrapper'  MyFileWrapper.cpp   
 Error  1   error C3673: 'MyFileWrapper' : class does not have a copy-constructor   MyFileWrapper.cpp   

对于第一个错误,智能感知给我以下错误,它有更好的解释:

    IntelliSense: no suitable user-defined conversion from "MyFileWrapper ^" to "MyFileWrapper" exists  

有什么问题,我该如何解决?

4

3 回答 3

2

gcnew MyFileWrapper()返回 a MyFileWrapper^(即对MyFileWrapper实例的托管引用),而不是 a MyFileWrapper

MyFileWrapper^ MyFileWrapper::ReadMyFile(System::String^ fileName)
{
    ...
    MyFileWrapper^ myFileWrapper = gcnew MyFileWrapper();
    ...
    return myFileWrapper;
}
于 2013-06-18T11:32:43.093 回答
1

您的返回类型和临时变量应该是MyFileWrapper^,毕竟它们是托管的。

于 2013-06-18T11:33:08.423 回答
0

C++/CLI 层中的所有托管类型都应该以TypeName ^的形式定义

于 2013-06-18T11:44:50.810 回答