I don't know how to initalize a static class member without creating an object of this class. Here is my code:
namespace {
class CPassant : public thread
{
private:
static unsigned LastID;
public:
CPassant (unsigned pDelaiArr = 0, unsigned pDelaiDep = 0)
{
(blabla)
}
static void setLastID (unsigned Valeur)
{
LastID = Valeur;
/* error : undefined reference to `(anonymous
namespace)::CPassant::LastID' */
} // setLastID ()
}; // class CPassant
} // anonym namespace
int main ()
{
CPassant::CPassant ().setLastID(0);
// doesn't work too:
// unsigned CPassant::LastID = 0;
return 0;
}
Thanks
NB: I've already looked at those answers, but none of them worked:
The problem with your initialization of LastID is that it's outside of the namespace you declared it. Put it in the same namespace and it will work.
You have declared, but not defined, the static member. You must define it. Here is one way:
Do this in your cpp file:
This is called defining the static class member, If you dont do this you will end up getting linker errors. You just declared the static member but did not define it.
Note that access specifiers do not matter here while defining the static member.
you have to do
in the .cpp file..unsigned CPassant::LastID = 0;