[CLOSED]C++, adding uniqueID to every class instance
-
wrote on 30 Oct 2013, 20:21 last edited by
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...Workout.H
@#ifndef WORKOUT_H
#define WORKOUT_H#include <QString>
#include <QList>
#include "interval.h"class Workout
{public:
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.CPP
@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)[Edit]
I used the code "here":http://www.learncpp.com/cpp-tutorial/811-static-member-variables/ that is supposed to work.. -
wrote on 30 Oct 2013, 20:36 last edited by
closed.. I will keep a pointer instead of a unique Id
Thanks -
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:
@
//whatever.h
class Whatever {
...
static int someInt; //this is the declaration
}//whatever.cpp
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:
@
//whatever.h
class Whatever {
int id;
public:
Whatever();
inline int getId() const { return id; }
};//whatever.cpp
Whatever::Whatever() {
static int counter = 1;
id = counter++;
}
@ -
wrote on 30 Oct 2013, 20:59 last edited by
Thanks a lot.. I used your second version and works fine
-
wrote on 31 Oct 2013, 16:42 last edited by
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 ...
-
wrote on 31 Oct 2013, 17:42 last edited by
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
1/6