You can't initialize the variable inside the class, because it technically hasn't been defined yet (only declared).
In the class, keep static const set<char> mySet;
.
In the implementation (i.e. .cpp file):
const char[] _MyClassSetChars = "ABC";
// Define and initialize the set, using
// the constructor that takes a start and end iterator
const set<char> MyClass::mySet(
_MyClassSetChars,
_MyClassSetChars + sizeof(_MyClassSetChars) / sizeof(_MyClassSetChars[0])
);
This also includes the terminating null character (\0
) in the set, which is probably not what you want -- instead, use this:
const set<char> MyClass::mySet(
_MyClassSetChars,
_MyClassSetChars + (sizeof(_MyClassSetChars) / sizeof(_MyClassSetChars[0]) - 1)
);
Note that here you can use <cstring>
's strlen()
(as per Let_Me_Be's answer) in place of the more general form above:
const set<char> MyClass::mySet(
_MyClassSetChars,
_MyClassSetChars + strlen(_MyClassSetChars)
);
Also see this question.