Help with static variables in class

Hi, I am not sure if this is the right forum If this isn’t I apologize in advance. But I was having some trouble with cling when it comes to using classes and static variables. look at the following example

class Animal{
    private:
        int height;
        int weight;
        string name;
        // Static variables are variables that are shared by all the objects of a class.
        static int numOfAnimals;
    public:
        // static int numOfAnimals;
        int getHeight(){ return height; }
        int getWeight(){ return weight; }
        string getName(){ return name; }
    
        void setHeight(int cm){ 
            height = cm;
            return;}
        void setWeight(int kg){ 
            weight = kg;
            return;}
        void setName(string animalName){ 
            name = animalName;
            return;}
    
        void setAll(int, int, string);
    
        Animal(int, int, string);
    
        ~Animal();
    
        Animal();
    
        static int getNumOfAnimals(){ return numOfAnimals; }
    
        void toString();
};
int Animal::numOfAnimals = 0;
e[1minput_line_15:2:14: e[0me[0;1;31merror: e[0me[1mdefinition or redeclaration of 'numOfAnimals' not allowed inside a
      functione[0m
 int Animal::numOfAnimals = 0;
e[0;1;32m     ~~~~~~~~^
e[0m


Interpreter Error:

Please move this declaration into a header that you then #include, or issue a .rawInput directive before and after these declarations.

1 Like

Thanks, this worked :slight_smile: Can you just briefly explain to me what does .rawInput does? And Is there a place where I can find some documentation on all the features in Cling, I am just starting out on it. Thanks a lot again.

We are currently re-working our documentation pages for ROOT; @couet could you consider looking at including cling info in the new doc?

Yes sure. That’s in the plans.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.