如何从c++/cli静态函数返回托管类
how to return a managed class from a c++/cli static function
我正在尝试使用c++/cli代码从c#调用非托管函数。
我有类似这样的代码:
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
是什么问题,我怎么解决它?
gcnew MyFileWrapper()
返回MyFileWrapper^
(即对MyFileWrapper
实例的托管引用),而不是MyFileWrapper
。
MyFileWrapper^ MyFileWrapper::ReadMyFile(System::String^ fileName)
{
...
MyFileWrapper^ myFileWrapper = gcnew MyFileWrapper();
...
return myFileWrapper;
}
您的返回类型和临时变量应该是MyFileWrapper^
,毕竟它们是管理的。
c++/CLI层的所有托管类型都应该以TypeName ^
相关文章:
- C++ 返回指向函数内定义的静态数组的指针是否有效?
- 为什么这些静态函数没有按预期从另一个线程返回?
- 从函数返回静态固定大小数组
- C++ 在静态函数中使用非静态变量返回类实例
- 无法从 pybind11 中的静态函数返回shared_ptr
- 没有返回类型的静态函数可以通过Windows上的编译,但不能传递Linux上的编译
- 返回本地静态的函数和返回静态成员的函数之间有什么区别(对象大小、性能等)?
- C++返回静态数组地址的函数
- 有没有更好的方法可以从静态和非静态函数返回相同的字符串文本
- 在静态函数中返回对象而不是构建对象的优点是什么?
- 如何调用函数来返回静态私有int
- 返回虚拟函数的静态成员,对于抽象类缺少VT
- 静态函数返回常数浮点
- C++函数,它接受成员函数并向其返回静态 lambda/std::function
- C++:函数返回静态库中的字符串给出错误:在令牌之前':'预期的"="、""、";"、'asm'或'__attribute__'
- 返回静态对象不同地址的函数
- CoCreateInstance在静态函数中返回EFAIL
- 函数返回静态变量C++
- 如何从c++/cli静态函数返回托管类
- 当从固定结构体中存储的对象构建函数时,c++ lambda构建静态函数返回意外值