Important: Please read the Qt Code of Conduct -

[CLOSED]C++, adding uniqueID to every class instance

  • This is a basic C++ question, I would like to add an unique ID to every instance of a class. I use to this so easily with Java, here i just wasted 1 hrs. grr
    Thanks if you can help...

    @#ifndef WORKOUT_H
    #define WORKOUT_H

    #include <QString>
    #include <QList>
    #include "interval.h"

    class Workout


    Workout(QList<std::shared_ptr<Interval>>, QString name, QString createdBy, Type type);
    int getID();

    private :

    static int s_nIDGenerator;
    int m_nID;


    #endif // WORKOUT_H@

    @Workout::Workout(QList<std::shared_ptr<Interval>> lstInterval, QString name, QString createdBy, Type type) {

    m_nID = s_nIDGenerator++;
    qDebug() << "ID HERE:" << m_nID;


    Error log :
    workout.obj:-1: error: LNK2019: unresolved external symbol "private: static int Workout::s_nIDGenerator" (?s_nIDGenerator@Workout@@0HA) referenced in function "public: __thiscall Workout::Workout(class QList<class std::tr1::shared_ptr<class Interval> >,class QString,class QString,enum Workout::Type)" (??0Workout@@QAE@V?$QList@V?$shared_ptr@VInterval@@@tr1@std@@@@VQString@@1W4Type@0@@Z)

    I used the code "here": that is supposed to work..

  • closed.. I will keep a pointer instead of a unique Id

  • Moderators

    In C++ there is a clearer separation of declaration and definition than in Java. You have declared the static s_nIDGenerator but you didn't define it.
    Declaration is part of the interface. Definition is part of implementation.

    Here's an example how to do it:
    class Whatever {
    static int someInt; //this is the declaration

    int Whatever::someInt = 1; //this is the definition

    But it's actually easier (and cleaner in my opinion) to use a static local variable than a static member. This way you'll have whole id code in one place:

    class Whatever {
    int id;
    inline int getId() const { return id; }

    Whatever::Whatever() {
    static int counter = 1;
    id = counter++;

  • Thanks a lot.. I used your second version and works fine

  • I think an id like that is limited because if you pass it to a function alone or even with the class type you cannot extract the class instance from it without an additional construct. imho you should learn and use what are handles? a handle is an identifier storing the address of the pointer by which you could dereference to the pointer itself (on net there is much confusion on this)

    for instance, in windows world in frameworks like MFC or even Qt handles are used for attaching their classes to the native window structures ...

  • Hey again Nicu,

    I will certainly check into that.
    My use of the unique ID was a workaround, I had to learn how work QModelIndex and finally i'm good without using id.
    Before I was using a QHash<key, object> and I wanted the key to be unique to get object fast retrieval "0(1)".

    See you

Log in to reply