-1
class blueprint1{
private:
    int serialnum;
    int static tracker=0;
public:
    blueprint1(){
        tracker += 1;
    }
    void output(){
        serialnum = tracker;
        cout << "The serial number of object is "<<serialnum<< endl;
    }
};

int main()
{

    blueprint1 one;
    one.output();
    blueprint1 two;
    two.output();
}

It giving me errorError 1 error LNK2001: unresolved external symbol "private: static int blueprint1::tracker" (?tracker@blueprint1@@0HA)

herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
NOMI
  • 23
  • 6

2 Answers2

2

You have to actual define your static class variable outside of your class declaration. So add to your cpp file the following line:

int blueprint1::tracker=0;

EDIT: sorry, i commited my answer by accident before writing the correct line of code

MikeMB
  • 20,029
  • 9
  • 57
  • 102
0

See initialization of static variable in code.
It should be initialized as int blueprint1::tracker = 0;

class blueprint1{
    private:
        int serialnum;
        int static tracker;
    public:
        blueprint1(){
            tracker += 1;
        }
        void output(){
            serialnum = tracker;
            cout << "The serial number of object is "<<serialnum<< endl;
        }
    };

    int blueprint1::tracker = 0; //this is the way you should initialize static variable 

    int main()
    {

        blueprint1 one;
        one.output();
        blueprint1 two;
        two.output();
    }
Pranit Kothari
  • 9,721
  • 10
  • 61
  • 137