-1

I'm trying to print the employee count after each created object but I'm getting an error like this: undefined reference to `Employee::numberofEmployees'. How can I solve this little problem? Any ideas? Btw I'm using a local class.

the purpose of the numberofEmployees variable is to store the information on the number of employee objects created/instantiated so far.

#include <iostream>
#include <string>

using namespace std;

class Employee{

public:
    string name;
    string surname;
    int year;
    double salary;
    static int numberofEmployees;


    Employee(int yil,string isim,string soyisim): year(yil),name(isim),surname(soyisim){
        numberofEmployees++;
    }

    Employee(){
        name = "not-set";
        year = 0;
        surname = "not-set";
        salary = 0.0;
    }

    void calculateSalary(){
        salary =  2310 + 2310 * year * 12 / 100.0;
    }

    void printInfo(){
        cout << name << " " << surname << " " << "(" << year << ")" << " " << salary << "TL/month" << endl;
    }
};

int main()
{
    Employee person1(4,"Berk","Kandemir");
    cout << Employee::numberofEmployees << endl;
    Employee person2(8,"Esat","Kandemir");
    cout << Employee::numberofEmployees << endl;
    Employee person3(20,"Paul","Walker");
    cout << Employee::numberofEmployees << endl;
    person1.calculateSalary();
    person2.calculateSalary();
    person3.calculateSalary();
    person1.printInfo();
    person2.printInfo();
    person3.printInfo();

    return 0;
}
mch
  • 9,424
  • 2
  • 28
  • 42
NOBUD
  • 27
  • 7
  • 1
    You forgot to initialize your `static` member. If you want to initialize it inside the definition, you should make it `inline` too. – Fareanor Mar 22 '21 at 11:57
  • you need to define the static member (you only declared it). Almost duplicate: https://stackoverflow.com/questions/3536372/defining-static-members-in-c – 463035818_is_not_an_ai Mar 22 '21 at 11:57
  • I wrote that like this with C++ 17 : static inline int numberofEmployees =0; still getting an error. – NOBUD Mar 22 '21 at 12:02
  • @Fareanor should I write like this ? static inline int numberofEmployees =0; – NOBUD Mar 22 '21 at 12:03
  • @NOBUD Yes. By convention, we often write `inline` before `static` but it think it shouldn't change anything. Check if you really have c++17 support at least. – Fareanor Mar 22 '21 at 12:07
  • @Fareanor I tried it with c++17 and c++20. I got an error like this 'numberofEmployees' declared as an 'inline' field inline static int numberofEmployees = 0; – NOBUD Mar 22 '21 at 12:12
  • @NOBUD Without the complete error message, we can't find what you did wrong. But making it `inline` works, if it isn't, then you should have made a mistake :) – Fareanor Mar 22 '21 at 12:16
  • rest of the message above – NOBUD Mar 22 '21 at 12:18
  • error: ISO C++ forbids in-class initialization of non-const static member 'Employee::numberofEmployees' mingw32-make.exe[3]: *** [CMakeFiles/untitled1.dir/main.cpp.obj] Error 1 mingw32-make.exe[2]: *** [CMakeFiles/untitled1.dir/all] Error 2 mingw32-make.exe[1]: *** [CMakeFiles/untitled1.dir/rule] Error 2 mingw32-make.exe: *** [untitled1] Error 2 – NOBUD Mar 22 '21 at 12:18
  • and this is my line : inline static int numberofEmployees = 0; – NOBUD Mar 22 '21 at 12:19

2 Answers2

1

You have to initialize your static data member outside the class using scope resolution operator something like this.

int Employee::numberofEmployees = 0;

This shows that you are accessing a static variable of class Employee and then initializing it.

0

If you have a static variable within a class, you are supposed to initialise it outside of the class, ie in a .cpp file. From C++17, however, you are allowed to initialise it in-class, in-line, using the inline keyword.

Code:

#include <string>
#include <iostream>

using namespace std;

class Employee
{

public:
    string name;
    string surname;
    int year;
    double salary;
    static inline int numberofEmployees = 0;

    Employee(int yil, string isim, string soyisim) : year(yil), name(isim), surname(soyisim)
    {
        numberofEmployees++;
    }

    Employee()
    {
        name = "not-set";
        year = 0;
        surname = "not-set";
        salary = 0.0;
    }

    void calculateSalary()
    {
        salary = 2310 + 2310 * year * 12 / 100.0;
    }
    void printInfo()
    {

        cout << name << " " << surname << " "
             << "(" << year << ")"
             << " " << salary << "TL/month" << endl;
    }
};

int main()
{
    Employee person1(4, "Berk", "Kandemir");
    cout << Employee::numberofEmployees << endl;
    Employee person2(8, "Esat", "Kandemir");
    cout << Employee::numberofEmployees << endl;
    Employee person3(20, "Paul", "Walker");
    cout << Employee::numberofEmployees << endl;
    person1.calculateSalary();
    person2.calculateSalary();
    person3.calculateSalary();
    person1.printInfo();
    person2.printInfo();
    person3.printInfo();

    return 0;
}

Or keep your code and add:

int Employee::numberofEmployees = 0;

After your class.

Output:

1
2
3
Berk Kandemir (4) 3418.8TL/month
Esat Kandemir (8) 4527.6TL/month
Paul Walker (20) 7854TL/month
BiOS
  • 2,282
  • 3
  • 10
  • 24
  • I tried this and I'm getting an error like this : – NOBUD Mar 22 '21 at 12:10
  • error: 'numberofEmployees' declared as an 'inline' field static inline int numberofEmployees = 0; – NOBUD Mar 22 '21 at 12:10
  • Which compiler are you using? You may try, in the meantime, with the second solution detailed in my answer, which is to keep your code but to add `int Employee::numberofEmployees = 0;` after the class – BiOS Mar 22 '21 at 12:12
  • Yes, the outside side method worked but another method not working for me. I'm using Clion. – NOBUD Mar 22 '21 at 12:13
  • Ok NOBUD, take a look [here](https://gcc.gnu.org/projects/cxx-status.html#cxx17), you will see that `inline` is only available from GCC7. I have it myself and I can confirm you that Clion Ide supports GCC and Clang. If you'd like to go for the `inline` approach, then consider upgrading your toolchain. You will easily find information on how to do this, and it will not take long – BiOS Mar 22 '21 at 12:21
  • Thank you for your contributions. Which IDE are you using? – NOBUD Mar 22 '21 at 12:26
  • You're very welcome. I am using the GCC compiler which I both started from Clion IDE and from the Terminal, it works for me as I have GCC > 7, so it should work for you too if you upgrade your toolchain. Which OS are you on? – BiOS Mar 22 '21 at 12:28
  • Currently, I'm using Windows 10. – NOBUD Mar 22 '21 at 12:30
  • Open a `cmd` window and type in `gcc --version`, what is the output? – BiOS Mar 22 '21 at 12:31
  • 'gcc' is not recognized as an internal or external command, operable program or batch file. – NOBUD Mar 22 '21 at 12:32
  • By following [this](https://www.jetbrains.com/help/clion/quick-tutorial-on-configuring-clion-on-windows.html) tutorial you should be able to upgrade the toolchain and use `inline` if you wish – BiOS Mar 22 '21 at 12:34
  • I did adding path ming\bin thing now it says "gcc (MinGW.org GCC-6.3.0-1) 6.3.0" – NOBUD Mar 22 '21 at 12:35
  • I'm gonna upgrade it via the link that you shared – NOBUD Mar 22 '21 at 12:36