我有VS2010 C#暴露一類2名靜態成員C++/CLI-> C#錯誤C2526:C聯動功能不能返回C++
public class Polygon
{
public static void Test(int test) {}
public static void Test(List<int> test) {}
}
我然後,創建一個控制檯建立了一個簡單的.NET DLL應用從VS2010 C++,並加入上述_tmain此功能
extern "C" void TestMe()
{
Polygon::Test(3);
}
添加參考和編譯給我此錯誤
1>WierdError.cpp(9): error C2526: 'System::Collections::Generic::List<T>::GetEnumerator' : C linkage function cannot return C++ class 'System::Collections::Generic::List<T>::Enumerator'
1> with
1> [
1> T=int
1> ]
1> WierdError.cpp(9) : see declaration of 'System::Collections::Generic::List<T>::Enumerator'
1> with
1> [
1> T=int
1> ]
1> WierdError.cpp(9) : see reference to class generic instantiation 'System::Collections::Generic::List<T>' being compiled
1> with
1> [
1> T=int
1> ]
我的一些意見:
- 它編譯成功,如果我刪除的extern「C」
- 它編譯成功,如果我重新命名
Test(List<int> test)
到Test2(List<int> test)
我的問題是,什麼錯誤,以及如何從C++端修復它。我現在的解決方法是重命名C#中的方法,但我寧願不必這樣做,我有一種感覺,我的C++項目中可能會缺少一個設置。
編輯:
我發現在C++更好的解決辦法,它看起來像我可以換另一個函數的調用.NET。
void wrapper()
{
Polygon::Test(3);
}
extern "C" void TestMe()
{
wrapper();
}
這似乎很愚蠢,我想知道這是否是一個編譯器錯誤?令我感到害怕的是使用這種方法,而且不得不擔心C#開發人員可能會在稍後添加這種靜態方法並破壞C++構建。
我不能重複這個錯誤。您使用什麼項目模板來創建C++/CLI項目?我懷疑你選錯了,因爲沒有一個CLR項目模板定義了一個'_tmain'入口點。 – ildjarn 2011-06-16 20:29:35
我認爲這是Win32控制檯,然後我進入設置並添加/ clr支持,我將在明天進行驗證。 – PeskyGnat 2011-06-17 00:47:27
http://www.youtube.com/watch?v=QUuQ-BdHflk-快進到2:08就是我所做的。 – PeskyGnat 2011-06-17 00:55:25