1

I am new to c++ and exploring the static feature on class members and functions.

person.h

#include <iostream>

using namespace std;

namespace school 
{

class Person 
{

    public:
        static const int MAX=99;

    private:    
        static int idcount;

    public:
        static void setID(int id) { idcount = id;}
        static int getID() { return idcount;}

    private:
        string name;    

    public:
        Person();
        Person(const Person &other);
        ~Person();  


};

}

Person.cpp

#include "person.h"

namespace school 
{

    //Constructor
    Person::Person()
    {
        this->name = "";
        cout << "object created" << endl;
    }

    //Copy Constructor
    Person::Person(const Person &other)
    {
        this->name = other.name;
    }

    //Destructor
    Person::~Person()
    {
        cout << "Destructor Called" << endl;
    }
}

main.cpp

#include "person.h"

int main()
{


    cout << school::Person::MAX << endl;
    school::Person::setID(5);
    cout << school::Person::getID() << endl;

    return 0;
}

i am getting the below linker error when i compile the above code. but when i change the idcount to public and declare it main ( int Person::idcount;) then i have no issues.

D:\Hari\Project\CPP_Practise\build>mingw32-make
[ 50%] Built target person
Scanning dependencies of target app
[ 75%] Building CXX object CMakeFiles/app.dir/chapter2/classes2.cpp.obj
[100%] Linking CXX executable app.exe
CMakeFiles\app.dir/objects.a(classes2.cpp.obj): In function 
`ZN6school6Person5se
tIDEi':
D:/Hari/Project/CPP_Practise/chapter2/person.h:21: undefined reference to 
`schoo
l::Person::idcount'
CMakeFiles\app.dir/objects.a(classes2.cpp.obj): In function 
`ZN6school6Person5ge
tIDEv':
D:/Hari/Project/CPP_Practise/chapter2/person.h:22: undefined reference to 
`schoo
l::Person::idcount'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\app.dir\build.make:97: recipe for target 'app.exe' failed
mingw32-make[2]: *** [app.exe] Error 1
CMakeFiles\Makefile2:103: recipe for target 'CMakeFiles/app.dir/all' 
failed
mingw32-make[1]: *** [CMakeFiles/app.dir/all] Error 2
Makefile:82: recipe for target 'all' failed
mingw32-make: *** [all] Error 2

What should i do when i am using it as private static variable?

hariudkmr
  • 191
  • 1
  • 12
  • 1
    You don't need change it to `private`, just define it as `int Person::idcount;`, better in `Person.cpp`. – songyuanyao Dec 01 '17 at 05:29
  • You haven't shown the error, but I'm going to guess it's covered by the bottom of this answer: https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix/12574407#12574407 – chris Dec 01 '17 at 05:30
  • yes adding it to the person.cpp - resolves the error, but just curious what is the theory behind it. so every static int, private or public should be declared in class definition? – hariudkmr Dec 01 '17 at 05:45

2 Answers2

3

1.Static variable means it is common for all the objects created for that class. 2.Whatever written in header file acts as a blueprint ie it doesnt allocate memory for it. For this reason all static variables are defined in cpp implementation file. Compiler allocates memory for those defined in cpp files.

  • so it doesnt matter whether it is private or pubic? – hariudkmr Dec 01 '17 at 06:12
  • It doesnt matter if it is private/public variable. Only static member of that class can access the private variable. You can try a sample app if you want. –  Dec 01 '17 at 06:26
0

Person::i still needs to be defined (outside the class body): int Person ::i;

Only those statics that aren't assigned a value inside the body need to be defined outside. And only non-volatile constant integrals can have such an in-body assignment.

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
  • to extend a bit - except const static variable all others whether private or public should be declared in the file which it is accessed? – hariudkmr Dec 01 '17 at 06:14