在托管C字符串数组++ [英] Arrays of strings in Managed C++
问题描述
我试着写在托管C ++应用程序,但我不能工作了如何声明字符串数组。
I'm trying to write an application in Managed C++, but I cannot work out how to declare an array of strings.
字符串^ linet [];
String^ linet[];
抛出一个错误
系统:字符串^':本机数组不能包含此托管类型
'System::String ^' : a native array cannot contain this managed type
所以我想有一个不同的方式来进行管理的数据类型做到这一点。到底是什么呢?
So I suppose there's a different way to do this for managed data types. What exactly is it?
推荐答案
你真的是托管C ++?不是C ++ / CLI?
Do you really mean Managed C++? Not C++/CLI?
您实际上正在使用C ++ / CLI(因为你张贴的错误消息)假设,有两种方法可以做到这一点:
Assuming you're actually using C++/CLI (because of the error message you posted), there are two ways to do this:
array<String^>^ managedArray = gcnew array<String^>(10);
将创建一个管理的阵列,即同一类型在C#中的String []。
will create a managed array, i.e. the same type as string[] in C#.
gcroot<String^>[] unmanagedArray;
将创建一个非托管C ++阵列(我从来没有实际上阵列尝试这一点 - 它与STL容器的效果很好,所以应该在这里工作,太)
will create an unmanaged C++ array (I've never actually tried this with arrays - it works well with stl containers, so it should work here, too).
这篇关于在托管C字符串数组++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!