2015-11-06 95 views
1

我有一個cv :: Point2f向量。我想要的是將這個向量從C++/CLI傳遞給C#。將STL/C++/CLI容器傳遞給.Net

由於我不能將cv :: Point2f存儲到std :: vector中,所以我使用了cliext :: vector。 然後,我不知道如何從C#得到它...

這是C++/CLI我的代碼:

是誰已經被定義CV :: Point2f的載體在其他地方初始化。

在C++/CLI,

cliext::vector<System::Drawing::Point> ManagedCPP::Points::get() { 
    cliext::vector<System::Drawing::Point> cliext_points; 

    for (auto &point : points) { 
     cliext_points.push_back(System::Drawing::Point(points.x, points.y)); 
    } 
    return corners; 
} 

在C#,

ManagedCPP mcpp = new ManagedCPP(); 
??? = mcpp.get_Points(); // what should be ??? 

或是否有任何的類型轉換需要的?

+1

http://stackoverflow.com/questions/21673874/c-cli-cliextvectort-as-return-type-of-public-class-function – SHR

+0

不,標題是好的,我已經睡:) 。對不起,我不介意:) – ipavlu

+1

請不要使用C++標記C++/CLI,謝謝(cc @Atomic) – Deduplicator

回答

4

您不能返回類型cliext :: vector < T>因爲它沒有公開聲明。但是可以將其轉換爲IEnumerable的< T>:

IEnumerable<System::Drawing::Point>^ ManagedCPP::Points::get() { 

    auto corners = gcnew cliext::vector<ystem::Drawing::Point>(); 
     for (auto &point : points) { 
     corners->push_back(System::Drawing::Point(points.x, points.y)); 
    } 
    return corners; 
    } 

或者,也可以返回一個標準的.NET容器(列表或陣列)。

array<System::Drawing::Point>^ ManagedCPP::Points::get() { 

    auto list = gcnew List<System::Drawing::Point>(points.size()); 
    for(auto & point: points) { 
     list->Add(System::Drawing::Point(point.x, point.y)); 
    } 

    return list->ToArray(); 
} 
+0

我想我可能是錯誤的向量,STL/C++可能是向量:**點** 。但我不完全確定,因爲它在問題中沒有定義。 – ipavlu