c++ - Define const string array in header -


i define in global header:

namespace fruitsaladapp {     enum fruits { banana, apple, orange, kiwi };     const char * fruitstrings[] { "banana", "apple", "orange", "kiwi" }; } 

i use header guards gets defined once per compilation unit.

i keep definition beside enum avoid getting them "out of sync" upon adding/deleting/modifying items, doing fruitstrings[banana] coherent , safe.

you did define array inside namespace, not in global scope. add static keyword before array definition , should work.

explanation: definition const variables declared in global namespace static, gives them internal linkage (they not visible in other .cpp files/translation units). const variables defined inside namespace not static - not necessary, since namespace limits visibility. when such header file included twice or more in project, symbol inside namespace being declared twice or more in namespace, gives error of multiple definition.


Comments