How do you create a global array of strings? I have tried declaring a string outside of main and dynamically initializing it using myString = new string[5];
, but I receive the error "No viable overloaded '='" in Xcode. What am I doing wrong?
Is this what you want?
char const *myStrings[] = {
"Some",
"Strings"
};
You need to use curly brace notation to initialize an array:
std::string myString[5] =
{
"string1",
"string2",
"string3",
"string4",
"string5"
};
If you're simply trying to declare and allocate it, all you need is:
std::string* myString = new std::string[5];
您是否已确定myStrings
是一个指针,即string* myStrings
而不是string myStrings
Your code snippet is a little terse, but if your full declaration is
string myString = new string[5];
then the problem is that you are trying to assign an array of strings to a variable that's only designed to hold one particular string. You'll want to do
string *myStrings = new string[5];
where the global variable contains a pointer to the first string in the array -- or
string myStrings[5];
where the global variable is the array.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.