0

I am struggling to understand the term static means and how it works. here I initialized a static variable "float percentage". and I have to convert it to percentage using convertToPercent() and after converting it I have to take the value and put the value to someMethod () to do some calculations

below is what I have

.h

class Foo     {

private:
    static float percentage;

public :    
    float convertToPercent();

};

.cpp

float Foo::convertToPercent() {
             percentage = (30/100) * 100;
          return percentage;
}

static float someMethod () {
  //place the static percentage value here after doing convertToPercent() method to do some calculation;
}

however it throws me an error message

Undefined symbols for architecture x86_64:
"Foo::percentage", referenced from:
 Foo::convertToPercent() in Foo.o

Apperciate for help given. thanks

user3493435
  • 81
  • 1
  • 3
  • 11

5 Answers5

4

static in this context means that the variable percentage does not belong to a particular instance of the class Foo, it is instead shared between all instances of Foo.

in order for the linker to know about the variable you need to define it and not just declare it that is why you get an error.

put

float Foo::percentage = 0;

in your .cpp file

AndersK
  • 35,813
  • 6
  • 60
  • 86
2

A static member is shared among all objects of that class.

A static data member is declared in the class declaration and is initialized in the file containing the class methods. The scope operator is used in the initialization to indicate to which class the static member belongs. However, if the static member is a const integral type or an enumeration type, it can be initialized in the class declaration itself.

class Foo     {
private:
    static float percentage; // this is declaration not definition

public :    
    float convertToPercent();

};

You need to define percentage in cpp file:

float Foo::percentage(0.0);  // initialize percentage to 0.0    

§ 9.4.2 Static data members

The declaration of a static data member in its class definition is not a definition and may be of an incomplete type other than cv-qualified void.

billz
  • 44,644
  • 9
  • 83
  • 100
2

You have to define the static variable outside of the class.

// header file
class Foo     {
private:
    static float percentage;
};

// cpp file
float Foo::percentage(0.0);

Because the member variable is static, there will only be one value of this variable shared amongst all instances of Foo classes (all Foo objects) throughout the runtime of your process. Eg. if you have three Foo objects A, B and C then changing the percent value in object B also changes it in objects A and C too.

Duncan Smith
  • 530
  • 2
  • 10
1

You only declared it in the class, but did not give a definition in the cpp that should look like this:

float Foo::percentage;
dornhege
  • 1,500
  • 8
  • 8
0

Below code of yours tries to declare a static member variable percentage --- OKAY

// header file
class Foo     
{
    private:
        static float percentage;       //constant declaration 
};

Below code of yours tries to assign a value to an object(percentage) which is not defined yet. -- NOT OKAY

// cpp file
float Foo::convertToPercent() {
         percentage = (30/100) * 100;     //tring to update percentage which is not defined yet.
      return percentage;                 //usage of object (percentage) which is not defined yet.
}

C++ requires that you provide a definition for anything you use but class-specific constants that are staic and of integral type are an exception. As long as you don't take their address, you can declare them and use them without providing a definition.

In your case, your implementation file (.cpp) should have the below line and then on you can use your "percentage" constant wherever applicable.

float Foo::percentage(0.0);     //definition of static class member : percentage
smRaj
  • 1,246
  • 1
  • 9
  • 13