C链接函数无法返回C ++类-由我的方法内容引起的错误 [英] C linkage function cannot return C++ class - error resulting from the contents of my method

查看:435
本文介绍了C链接函数无法返回C ++类-由我的方法内容引起的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在导出可以从非托管代码中调用的方法,但是该函数本身位于托管c ++项目中.以下代码导致编译器错误:

I am exporting a method that can be called from unmanaged code, but the function itself lives in a managed c++ project. The following code results in a compiler error:

error C2526: 'System::Collections::Generic::IEnumerator<T>::Current::get' : C linkage function cannot return C++ class 'System::Collections::Generic::KeyValuePair<TKey,TValue>'
error C2526: 'System::Collections::Generic::Dictionary<TKey,TValue>::KeyCollection::GetEnumerator' : C linkage function cannot return C++ class 'System::Collections::Generic::Dictionary<TKey,TValue>::KeyCollection::Enumerator'

extern "C"
__declspec( dllexport )
bool MyMethod(std::string &name, std::string &path, std::map<std::string, std::string> &mymap)
{
  System::Collections::Generic::Dictionary<System::String ^, System::String^> _mgdMap = gcnew System::Collections::Generic::Dictionary<System::String ^, System::String ^>();

  // Blah blah processing
}

稍微研究一下该错误,问题通常与标记为"extern"C"的方法的定义有关.那么,为什么要完全关心该方法内部发生的事情呢?

Looking into this error a little bit, the issue normally has to do with the definition of the method that is marked as 'extern "C"'. So why would it be at all concerned with what goes on inside the method?

如果我注释掉Dictionary的初始化,或者将其切换到HashTable,则所有内容的编译效果都很好.

If I comment out the Dictionary initialization, or switch it to a HashTable everything compiles beautifully.

以下内容也适用-如果我不是在本地定义字典,而是通过在方法中对其进行初始化来避免使用局部变量.

The following works as well - if instead of defining a dictionary locally, I avoid the local variable by initializing it in a method.

bool status = CallAnotherMethod(ConvertToDictionary(mymap));

其中ConvertToDictionary声明为

where ConvertToDictionary is declared as

System::Collections::Generic::Dictionary<System::String ^, System::String ^>^ ConvertToDictionary(std::map<std::string, std::string> &map)
{
}

这告诉我这是一个看似任意的错误.我仍然想了解为什么编译器认为这是一个问题.

Which tells me that this is a seemingly arbitrary error. I would still like to understand why the compiler thinks this is a problem.

推荐答案

很抱歉,您必须为死者张贴尸体,但我需要和别人分享我的幸福:)

Sorry for necroposting but i need to share my happiness with somebody :)

似乎您只需创建两个包装即可解决此问题-一个包装用于标有"extern C"的外部调用,另一个包装用于内部调用.在这种情况下,外部C"之外的所有内容都将照常执行.NET代码.

Seems that you are able to solve this just by creating two wrappers - one for external call marked with "extern C" and one for internal call. In this case everything outside "extern C" will be executed as usual .NET code.

topicstarter在这里回答了-

That was answered by the topicstarter here - C++/CLI->C# error C2526: C linkage function cannot return C++ class

void DummyInternalCall(std::string &name, std::string &path, std::map<std::string, std::string> &mymap)
{
   System::Collections::Generic::Dictionary<System::String ^, System::String^> _mgdMap =  gcnew System::Collections::Generic::Dictionary<System::String ^, System::String ^>();

  // Blah blah processing
}

extern "C" __declspec( dllexport )
bool MyMethod(std::string &name, std::string &path, std::map<std::string, std::string> &mymap)
{
   DummyInternalCall(name, path, mymap);
}

这篇关于C链接函数无法返回C ++类-由我的方法内容引起的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆