I try to declare a structure with static variable.
I got a example from stack overflow but it doesn’t work in ROOT.
Here are the example and the error I got.
Jerry
class A
{
public:
static int x;
};
int A::x = 100;
ROOT_prompt_4:1:8: error: definition or redeclaration of ‘x’ not allowed inside a function
int A::x = 100;
Note that you can only have one variable A::x. So usually you put class A { ... }; in a header file (and add include guards) while the int A::x = 100; goes into the corresponding cpp file (.L this file just once).
Could it be that you are reloading the file or including it multiple times? As Olivier showed, it also works fine if you have everything in one file.
Final question: are you sure you really need a public static variable? It feels like a global variable - and those should be avoided.
With C++17 (not available in Ubuntu 14), you could use an inline static variable instead.