1

I tried to initialize an array of string in class as following:

class Haab{
    string str[];
    Haab(){
        str[] = {"abc", "abd", "abe"};
    }
};

But the Devc++ 5.6.1 reports a warning:

[Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]

Is this way of initializing arrays in class illegal? If so, how to properly initialize the array? Thank you.

5
  • As the warning says, its a new feature from C++11 (see: here). C++03, the previous standard, only allowed initializer-list syntax to be used with POD data types (which string is not). Commented Dec 7, 2014 at 3:49
  • I changed "string" to "char" but it reported the same warning. By the way what is initializer-list syntax? Commented Dec 7, 2014 at 3:51
  • Looking at your code, again, is that the actual code you are trying to compile? str[] = {"abc", "abd", "abe"}; isn't even valid syntax as far as I know (separate from the declaration that is). Commented Dec 7, 2014 at 3:55
  • Yes I tried to compile that code. Then what is the correct syntax to do initialize such array? Commented Dec 7, 2014 at 4:08
  • As an addendum to Alf's answer, if you do syntax like char str[] in a struct, its probably a flexible array member (not valid in C++) Commented Dec 7, 2014 at 4:22

1 Answer 1

2

The given code,

class Haab{
    string str[];
    Haab(){
        str[] = {"abc", "abd", "abe"};
    }
};

is invalid in a number of ways:

  • string str[]; declares a member array of unknown size. Can't do that.

  • In str[] = {"abc", "abd", "abe"};, the expression str[] uses the [] indexing operator without specifying the index.

  • If that parsed, then the = would denote assignment of a single string.

Here's one C++03 way to do things:

#include <string>
#include <vector>

namespace qwe {
    using std::string;
    using std::vector;

    class Haab
    {
    private:
        vector<string> str_;

    public:
        Haab()
        {
            static char const* const data[] = {"abc", "abd", "abe"};

            for( int i = 0; i < 3; ++i )
            {
                str_.push_back( data[i] );
            }
        }
    };
}  // namespace qwe

There are also other C++03 ways, all of them ugly (as the above).

In C++11 and later it can be done with more elegant & simple notation;

#include <string>
#include <vector>

namespace qwe {
    using std::string;
    using std::vector;

    class Haab
    {
    private:
        vector<string> str_;

    public:
        Haab()
            : str_{ "abc", "abd", "abe"}
        {}
    };
}  // namespace qwe

but this does still not compile with Visual C++ 13.0 (it does compile with MinGW g++ 4.9.1).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. No warnings from compiler now. There's still a question though. In line16 you wrote 'static char const* const data[]='. What does "const* const" mean? (I also tried const char* data[] and it works. Are the two expressions equivalent?)
@ZiningZhu: If you had not omitted the second const they would have been equivalent. I use the syntax T const because that's the general syntax, e.g. it's what you have to use for the second const. But for the basic type of the declaration you can write const T for readability, and many prefer that even though it doesn't generalize; in particular it's done in all the standard's examples.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.