How to define an array of strings in C ++ / CLI?

What is wrong with this:

I get these errors for all 5 definitions:

 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."
}
+3
source share
2 answers

Cannot be used gcnewinside an array initializer:

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."
};
+5
source

The other responder has the correct syntax, but that is not because you are in an array initializer.

There are two errors with string initialization.

  • When using gcnew you do not need to enable it ^. You are creating a new object, not a new link.
  • When calling the constructor, you need parentheses.

So, the correct constructor syntax should have called gcnew String("Madam I'm Adam.").

, , . String, . , new String("Madam I'm Adam.") #: , new String .

+3

All Articles