Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C联动函数不能返回C++;类-由于我的方法的内容而导致的错误_C++_Extern_Managed C++_Linkage_Vc90 - Fatal编程技术网

C++ C联动函数不能返回C++;类-由于我的方法的内容而导致的错误

C++ C联动函数不能返回C++;类-由于我的方法的内容而导致的错误,c++,extern,managed-c++,linkage,vc90,C++,Extern,Managed C++,Linkage,Vc90,我导出了一个可以从非托管代码调用的方法,但是函数本身存在于托管C++项目中。以下代码导致编译器错误: error C2526: 'System::Collections::Generic::IEnumerator<T>::Current::get' : C linkage function cannot return C++ class 'System::Collections::Generic::KeyValuePair<TKey,TValue>' error C252

我导出了一个可以从非托管代码调用的方法,但是函数本身存在于托管C++项目中。以下代码导致编译器错误:

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'
其中ConvertToDictionary声明为

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

这告诉我这是一个貌似武断的错误。我仍然想理解编译器为什么认为这是个问题。

< p>如果你写C++中的函数,那就是从C调用,你不能在它的接口中使用任何东西(参数和返回类型)这里不是C.,你的论点是C++对象。

< p>对不起,我要和别人分享我的快乐:

似乎您可以通过创建两个包装器来解决这个问题—一个用于标记为“extern C”的外部调用,另一个用于内部调用。在这种情况下,“extern C”之外的所有内容都将像.NET代码一样执行

这里的话题发起者回答了这个问题-

void DummyInternalCall(std::string和name,std::string和path,std::map和mymap)
{
System::Collections::Generic::Dictionary _mgdMap=gcnew System::Collections::Generic::Dictionary();
//胡说八道
}
外部“C”\u declspec(dllexport)
bool MyMethod(std::string和name,std::string和path,std::map和mymap)
{
DummyInternalCall(名称、路径、mymap);
}

std::参数均未导致编译错误。这不是问题。@Liz:那可能是你的编译器在其他地方出错了。在C++代码>外部“C”<代码>链接函数原型中,不能使用C++构造。例如,所有这些引用:如何从用C编写的编译单元传递这些引用?C不知道参考资料。你能处理好吗?似乎我也有类似的问题-无法从C++/CLI初始化C#类,我认为这是因为“extern C”要求编译器在创建字典或类之前知道它的大小,“C”语言的规则-接口在实现之前进行,它告诉编译器这个对象需要初始化多少内存,Hans Passant在这里回答说,我的类是用C#定义的,我不想在C++/CLI中复制它的实现-我如何告知C++/CLI这个类需要什么大小?是的,这也是我在问题中说的。这似乎不太合乎逻辑,但确实奏效了。看看你链接的问题,我想这里的解释是有道理的。
bool status = CallAnotherMethod(ConvertToDictionary(mymap));
System::Collections::Generic::Dictionary<System::String ^, System::String ^>^ ConvertToDictionary(std::map<std::string, std::string> &map)
{
}
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);
}