33

I am having trouble initializing a constant array of constant strings.

From week.h (showing only relevant parts):

class Week {
  private:
    static const char *const *days = { "mon", "tue", "wed", "thur",
                                       "fri", "sat", "sun" };
};

When I compile I get the error "excess elements in scalar initializer". I tried making it type const char **, thinking I messed up the 2nd const placement, but I got the same error. What am I doing wrong?

7
  • Why do you have two pointer declarations? Commented Jun 30, 2011 at 15:02
  • Not really sure at all, but does static const char * days[] = { help or will it break your strings from being const? Commented Jun 30, 2011 at 15:03
  • I second Tim's question! Commented Jun 30, 2011 at 15:03
  • 2
    @Tim : If a C-string is a char*, then a C-array of C-strings must be char**. Commented Jun 30, 2011 at 15:05
  • 1
    "An array of C-strings is type char**" No it's not. You are confusing arrays with pointers. As is ildjarn, apparently. Commented Oct 31, 2014 at 14:19

2 Answers 2

56

First of all, you need an array, not a pointer.

static const char * const days[] = {"mon", "tue", "wed", "thur",
                                       "fri", "sat", "sun"};

Second of all, you can't initialize that directly inside the class definition. Inside the class definition, leave only this:

static const char * const days[]; //declaration

Then, in the .cpp file, write the definition

const char * const Week::days[] = {"mon", "tue", "wed", "thur",
                                       "fri", "sat", "sun"};

Update for C++11 Now you can initialize members directly in the class definition:

const char * const days[] = {"mon", "tue", "wed", "thur",
                                       "fri", "sat", "sun"};
Sign up to request clarification or add additional context in comments.

2 Comments

Actually you can update your answer for C++11, so you can do it in the class declaration.
Is a static array like this what you'd recommend for a set of strings like these weekday names? Is there a best practice for this sort of situation?
19

For C++11, you can make the initialisation inside your class declaration, in your .h file. However, you will need to include constexpr in your .cpp file too. Example for the case above:

In your week.h file, write:

class Week {
    public:        
       static const constexpr char* const days[] = 
           { "mon", "tue", "wed", "thur","fri", "sat", "sun" };
};

In your week.cpp file, write somewhere:

constexpr const char* const Week::days[];

Make sure you enable C++11, e.g. compile with

g++ -std=c++11 week.cpp

2 Comments

This was very helpful. Do you know the fundamental reason why the bit in the .cpp file is necessary? This looks very strange and unlike typical c++ (the declaration looks like a definition and vice versa). My best guess is that the in-class initialization is initializing the elements of the array, but not the array itself, and that's what the .cpp part is doing, but I have no real faith in this. In general in C++11, I thought it was possible to initialize static constexpr's of any time purely in the class definition so I don't see why the .cpp part is necessary at all.
The first const is redundant I believe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.