views:

154

answers:

6

Hi,

I'm a programming student in my first C++ class, and for a recent project I did, I was unable to create an array of strings like I could do in C#:

string MONTHS[ARRAY_CAPACITY] = { "Jan", "Feb", "Mar", "April", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec" };
// this yields many compiler errors in C++

Is it possible to do something similar in C++?

Thanks!

+6  A: 

Yes, it does:

#include <string>

int main(void)
{
    static const size_t Capacity = 12;
    std::string Months[Capacity] = { "Jan", "Feb", "Mar", "April", "May",
                                        "June", "July", "Aug", "Sep", "Oct",
                                        "Nov", "Dec" };
}

Your errors were probably related to something else. Did you remember to use std::? Without knowing, it could be anything. Was Capacity the wrong size? Etc.

Note your code wasn't actually a constant array. This is:

#include <string>

int main(void)
{
    static const size_t Capacity = 12;
    static const std::string Months[Capacity] = { "Jan", "Feb", "Mar", "April",
 /* ^^^^^^^^^^^^ */                                 "May", "June", "July", "Aug",
                                                    "Sep", "Oct", "Nov", "Dec" };
}

Also, you don't actually need Capacity, as others will show, and you could use const char* if you'd like, though you lose the std::string interface.

GMan
+3  A: 

If you initialise the array in C++ then it doesn't require a size to be set (although it'll accept one), so:

 std::string months[] = { "Jan", "Feb", "Mar", "April", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec" };

compiles fine with g++ for me and I'd expect it to compile elsewhere too. I expect your errors are due to the lack of std:: namespace.

Mike Anchor
I was missing a colon in my ARRAY_CAPACITY declaration! Figures, as soon as I post here I figure out my problem. Sorry to bother you SO'ers with such an elementary question. Thanks for providing me with the correct syntax!
Alex
Make that a semicolon, my apologies.
Alex
A: 

Yes. The syntax you used in the question is correct, as long as the compiler understands that string is std::string and as long as the number of initializers in between {} does not exceed ARRAY_CAPACITY.

Of course, if you wanted to have a constant array, as the title suggests, you should have declared it const. Without const your array will have external linkage and cause linker errors if you put it into a header file included into multiple translation units.

const std::string MONTHS[ARRAY_CAPACITY] = { "Jan", /* and so on ... */ };
AndreyT
A: 

The preferred method for an array of constant strings would probably be an array of cstrings,

const char* MONTHS[] = { "Jan", "Feb", "Mar", "April", "May", "June", "July", 
    "Aug", "Sep", "Oct", "Nov", "Dec" };

However, it can also be done with std::strings,

const string MONTHS[] = { string("Jan"), string("Feb"), ... };

Some compilers may not allow implicit conversion from char* to std::string when you initialize an array with curly braces; explicitly assigning an std::string constructed from a char* will fix that.

dauphic
A: 

Yes, Visual C++ supports it - I've just done something similar. Not sure about other versions of C++.

Have you included the library? What is the definition of ARRAY_CAPACITY?

When you say 'unable', do you mean you had compiler/linker errors or something else? Can you provide more details?

GenericMeatUnit
A: 

I assume that you want to use std::string, but in C++ this is not possible. Or at least not this simple. You could use instead of the std::string a simple const char* MONTH[ARRAY_CAPACITY] = {"Jan", ...}; This might already does the trick for.

If you really want to use std::string you might want to take a look at the STL container and the boost library( www.boost.org ). Especially boost::assign which can do something similar like you want to do.

Regards, Michael

mkaes