C ++ - Implementation of static class cpp

1

Hello! I was trying to implement a Static class but I had the following error: "undefined reference to math :: cod" but variable cod is in .h follows the code below class .h and cpp;

#ifndef MATEMATICA_H
#define MATEMATICA_H


class matematica
{
  public:
    static int getCod();
    static void setCod(int);

  protected:

  private:
    static int cod;

};

#endif // MATEMATICA_H 

CPP CLASS:

#include "matematica.h"


void matematica::setCod(int c)
{
   matematica::cod = c;
}

I do not know what to do.

    
asked by anonymous 29.11.2018 / 02:13

1 answer

1

In the header file you are only declaring the static variable. In other words you are just giving a name and a type. The variable is not "created". Somewhere else (for example, in the corresponding .cpp file) you need to define the variable, that is, to actually create it.

Just think of this: if the variable was not a static variable, it would not exist until an instance of the class was created.

TLDR;

In the corresponding .cpp file do:

int matematica::cod = 0;

    
29.11.2018 / 15:27