I have two classes with a constants.
For example there is a class called class_a.m contain a constant kWidth = 150,
also I have a class called class_b.m caontain a constant kWidth = 200
After run my project I get an error with duplicate symbol, but these files are not nested (i mean class_a into class_b or class_b into class_a). Also i use this constantin implementation only.
Source:
const int kWidht = 150;
Error description:
ld: duplicate symbol _kWidht...
Thanks for help!
If the constant is only used within that single implementation file, then you should prefix its declaration with
static. That is, turn this:into this:
The
statickeyword tells the compiler that this symbol is only used within the current file.1 Without it, the compiler assumes you’re declaring a global variable, one that could be accessed from anywhere in the final application. Declaring two global variables with the same name isn’t a good idea as you’d have no way to distinguish between them, so the compiler rightly complains. Luckily, it’s easy to fix this by simply being more explicit about your intentions via thestatickeyword.1: More accurately “translation unit”, but “file” is good enough for the purposes of this question.