English 中文(简体)
• 如何界定C++/CLI的阵列?
原标题:How to define an array of strings in C++/CLI?

这是错误的:

i m 在所有5项定义中都发现这些错误:

 error C3698:  System::String ^  : cannot use this type as argument of  gcnew 
 error C2512:  System::String::String  : no appropriate default constructor available    



array<String^>^ arr = gcnew array<String^>
{
    gcnew String^ "Madam I m Adam.",    
    gcnew String^ "Don t cry for me,Marge and Tina.",   //error C2143: syntax error : missing  }  before  string    AND error C2143: syntax error : missing  ;  before  string 
    gcnew String^ "Lid off a daffodil.",
    gcnew String^ "Red lost Soldier.",
    gcnew String^ "Cigar? Toss it in a can. It is so tragic."
}
最佳回答

页: 1 在阵列初始器内:

array<String^>^ arr = gcnew array<String^> {
    "Madam I m Adam.",    
    "Don t cry for me,Marge and Tina.",
    "Lid off a daffodil.",
    "Red lost Soldier.",
    "Cigar? Toss it in a can. It is so tragic."
};
问题回答

另一名答复者有正确的辛迪加,但这不是因为你重新加入阵列的初衷。

你的直截了当的初衷有两个错误。

  1. When using gcnew, you don t need to include the ^. You re constructing a new object, not a new reference.
  2. You need parentheses when calling the constructor.

因此,适当的建筑群子将打电话gcnew String(Madam I m Adam)

然而,正如其他答复者指出的那样,你不需要这样做。 狭义的字面已经是一个强硬物,因此,你可以取消对施工者的呼吁,并直接使用直面字面。 这与C#中的“new String”(“Madam I m Adam”)new String





相关问题
Managed C++ to form a bridge between c# and C++

I m a bit rusty, actually really rusty with my C++. Haven t touched it since Freshman year of college so it s been a while. Anyway, I m doing the reverse of what most people do. Calling C# code ...

Is it difficult to port C++ to C++/CLI?

I suppose you cannot simply compile a C++ application with a C++/CLI compiler. I am wondering if it would be difficult. Has anybody tried this, and if so: were there a lot of modifications needed?

How can I use Code Contracts in a C++/CLI project?

I recently stumbled upon Code Contracts and have started using them in my C# projects. However, I also have a number of projects written in C++/CLI. For C# and VB, Code Contracts offer a handy ...

Creating 64 bit CLR C++ projects in VS2008

I am creating a wrapper around a native lib, which comes in both 32 & 64 bit flavors. I have a fairly complex C++/CLR project that includes a number of header files from the native libs. I got it ...

How to return an array of .NET objects via a COM method

I have a .NET assembly. It happens to be written in C++/CLI. I am exposing a few objects via COM. Everything is working fine, but I cannot for the life of me figure out how to return an array of my ...

in C++/CLI

When I m trying serialize a class containing this property: [NonSerialized] property System::Collections::ObjectModel::ReadOnlyCollection<String^>^ IgnoredWords I get a compilation error ...

热门标签