简体   繁体   English

一个列表 <Tuple<int, float> &gt;在托管C ++中

[英]A List<Tuple<int, float>> in managed C++

I'm working on some software that is written in native C++ with a managed C++ wrapper and the user interface in C#. 我正在研究一些使用托管C ++包装器和C#中的用户界面用本机C ++编写的软件。 I need to pass some information from the native code all the way up to the interface. 我需要将一些信息从本机代码传递到接口。 The best way I can think to pass my information is in a list of tuples. 我认为传递信息的最佳方式是在元组列表中。 I understand that in the native C++ I need to use a list<tuple<..>> and that works fine. 我知道在本机C ++中我需要使用list<tuple<..>>并且工作正常。 What I want to do now, is take that output in the wrapper, and return a List<Tuple<..>> which is the System::Collections::Generic::List and System::Tuple instead of the stl ones from the native. 我现在要做的是,在包装器中获取该输出,并返回一个List<Tuple<..>> ,它是System::Collections::Generic::ListSystem::Tuple而不是stl来自本地的。 I know the lists are very different syntactically but that shouldn't be the issue. 我知道列表在语法上是非常不同的,但这应该不是问题。 In the C# code a List<Tuple<..>> is accepted by the compiler but in the managed C++ code it is not. 在C#代码中,编译器接受List<Tuple<..>> ,但在托管C ++代码中则不然。 Am I doing this wrong/am I using the wrong data types? 我这样做错了/我使用了错误的数据类型吗? Any help would be awesome! 任何帮助都是极好的!

Try something like this: 尝试这样的事情:

using namespace System::Collections::Generic;

// A List<Tuple<int, float>> in managed C++
public ref class TupleTest
{
public:
    static List<Tuple<int, float>^>^ GetTuple() {
        List<Tuple<int, float>^>^ ret = gcnew List<Tuple<int,float>^>();
        Tuple<int,float>^ t = gcnew Tuple<int,float>(5, 2.6);
        ret->Add(t);
        return ret;
    }
};

Note: you could use Int32 and Single (or Double) instead of int/float if you like. 注意:如果您愿意,可以使用Int32和Single(或Double)代替int / float。

EDIT: Notice the ^ operators. 编辑:注意^运算符。 Those denote C++/CLI reference types. 这些表示C ++ / CLI引用类型。 You'll be using them a lot! 你会经常使用它们! (It's kinda like * for pointers in regular C++, but means GC'ed reference type) (它有点像*常规C ++中的指针,但意味着GC的引用类型)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM