1

I would like to define something like that in a global header:

namespace FruitSaladApp
{
    enum Fruits { Banana, Apple, Orange, Kiwi };
    const char * fruitStrings[] { "Banana", "Apple", "Orange", "Kiwi" };
}

I use header guards so that it gets defined only once per compilation unit.

I would like to keep the definition beside the enum to avoid getting them "out of sync" upon adding/deleting/modifying items, so that doing fruitStrings[Banana] is coherent and safe.

2
  • I can't replicate it. Can you please try to create a Minimal, Complete, and Verifiable Example and show us? Also please show the complete error output (preferably the whole build log) from the MCVE. Commented Jan 29, 2016 at 8:47
  • You are right, a MCVE works here... I added a detailed error message from minGW linker Commented Jan 29, 2016 at 8:52

3 Answers 3

3

You did define this array inside a namespace, not in a global scope. Add static keyword before array definition and it should work.

Explanation: By definition const variables declared in global namespace are static, so that gives them internal linkage (they are not visible in other .cpp files/translation units). But const variables defined inside namespace are not static - this is not necessary, since namespace itself limits their visibility. But when such header file is included twice or more in project, then symbol inside namespace is being declared twice or more in that namespace, which gives error of multiple definition.

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

2 Comments

Thanks, could you clarify why the namespace changes the behavior? I thought namespaces were just a language name prefixing convenience.
@piotrsmaron The OP says there are header guards, so if they work this should not be an issue.
1

I thought that multiple definitions of const variables in c++ was allowed from this question

That question, and internal linkage in general, implies that you may define them once in multiple translation units each.

You will still get multiple definition error if you define it multiple times in a single translation unit. Solution: Use header guards... Since you claim to use header guards, then the problem is either: incomplete example, or mistake in the header guards, or bad compiler.

3 Comments

I do use header guards, and the error is at link time.
@galinette then you'll need to provied a minimal reproducible example with the error message. I tried your program in two different cpp files compiled and linked together, and got no errors.
See my answer, I could not reproduce it either in a MCVE and in fact I was a victim of qmake horrible incremental build behavior. Maybe time to switch to cmake.
0

Actually, it requires the pointer to be const, not the pointed-to chars

namespace FruitSaladApp
{
    enum Fruits { Banana, Apple, Orange, Kiwi };
    const char * const fruitStrings[] { "Banana", "Apple", "Orange", "Kiwi" };
}

The initial question contained already that const but due to qmake, which is sometimes awkward for handling incremental build, some cpp files which included that header indirectly were not recompiled. Hence the error which persisted.

The effect of the second const was confirmed by a MCVE

Comments

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.