Static variable class member

HI All

I am doing some practice tests with classes. I wrote this simple class:

class Person{

private: 
  String _name;
  int _age;
  int _size;

public:
  Person(){
    _size = 48;
  }
  Person(String name){
    _name = name;
    _size = 48;
  }
    Person(String name, int size){
    _name = name;
    _size = size;
  }


 int setName(String name){
    _name = name;
  }

  int setAge(int age){
    _age = age;
  }

  int setSize(int size){
    _size = size;
  }

  void showData(){
    Serial.print("this person's name is : ");
    Serial.println(_name);
    Serial.print("this person's age is : ");
    Serial.println(_age);
    Serial.print("this person's size is : ");
    Serial.println(_size);
  } 
};


void setup() {
  Serial.begin(9600);
Person john("John"); 
john.setAge(45);
john.showData();

}

void loop() {
}

Now, I would like the private variable _size to be the same for all objects of the class. I have tried to use the keyword static in front of it but it throws errors.

My understanding is that a static variable would act like a global variable for the class.
I may want to use it as a constant in certain scenarios or as a variable in others, but it has to be the same for all the objects of the class.
Any suggestions?
Cheers.

Try this:

class Person{

private: 
  String _name;
  int _age;
  static int _size;

public:

  Person(){
    _size = 48;
  }
  Person(String name){
    _name = name;
    _size = 48;
  }
    Person(String name, int size){
    _name = name;
    _size = size;
  }


 int setName(String name){
    _name = name;
  }

  int setAge(int age){
    _age = age;
  }

  int setSize(int size){
    _size = size;
  }

  void showData(){
    Serial.print("this person's name is : ");
    Serial.println(_name);
    Serial.print("this person's age is : ");
    Serial.println(_age);
    Serial.print("this person's size is : ");
    Serial.println(_size);
  } 
};

int Person::_size=0;

void setup() {
  Serial.begin(9600);
Person john("John"); 
john.setAge(45);
john.showData();

}

void loop() {
}

please show the code where you use "static"

please see belwo:

@wildbill this is exactly what I was trying but it throws errors...

inline static int _size;

Also in your Wokvi you have a several errors "no return in function declared non-void" in your calss methods setName, setAge and setSize

Please post the code here. It's easier that way. And post the error message as well. It's a lot easier to find an error when you have the message.

In your WOWKI you're missing this line that defines the static variable. Since it is static it doesn't get defined when you create an instance. So you have to have code to define it.

think it needs to be inline..

add inline before static..

good luck.. ~q

@wildbill , apologies, you were right I missed the line "int Person::_size=0;" of your code
@Delta_G Thanks
this works! So static variables HAVE to be defined outside the class ?
I was trying to have it initialized by the constructor. I have noticed that if I add
int Person::_size=0;
then also the constructor initialization works... I will paste the code and output below...

@b707 you are right, strangely it does not given an error about that and the functions work as expected though

class Person{

private: 
  String _name;
  int _age;
  static int _size;

public:

  Person(){
    _size = 48;
  }
  Person(String name){
    _name = name;
    _size = 48;
  }
    Person(String name, int size){
    _name = name;
    _size = size;
  }


 int setName(String name){
    _name = name;
  }

  int setAge(int age){
    _age = age;
  }

  int setSize(int size){
    _size = size;
  }

  void showData(){
    Serial.print("this person's name is : ");
    Serial.println(_name);
    Serial.print("this person's age is : ");
    Serial.println(_age);
    Serial.print("this person's size is : ");
    Serial.println(_size);
  } 
};

int Person::_size=0;

void setup() {
  Serial.begin(9600);
Person john("John"); 
john.setAge(45);
john.showData();

}

void loop() {
}


output
This person's name is : John
this person's age is : 45
this person's size is : 48

It's called undefined behavior. Sometimes it works. Sometimes it crashes. Sometimes weird stuff happens.

If you're not going to have the function return a value then the function should be declared as void:

void setName(String name){
    _name = name;
  }

Thank you all😊

It depends on Arduino IDE settings, that are different for a different boards. When I selected a Raspberry Pi RP2040 in order to compile your code - it throw me that errors.

When you compile for other boards it generated a warning. But for some boards those warnings are treated as errors. It just depends on the compiler settings.

@b707
@Delta_G
got it! thanks! :slight_smile: