c++

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.

Related Question How do you add strings to an array of strings How do you convert an array of strings to an array of floats in c++? How do you create a reference to an array at a certain address? How do you create an array of member function pointers with arguments? How do you create an array of binary search trees? How do you “declare” a global struct? How do you create a private dynamic array in a c++ class? How do you prioritize global constructors? How do you declare a global std::vector 2d array across multiple files? c++ how to create a new array of strings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM