Possible Duplicate:
Resolving a linker error: undefined reference to static class members
This declaration: static unsigned NbCPassant; only works when it’s outside the class. If I put it inside, (to have it private) this doesn’t compile, with the error:
undefined reference to `(anonymous namespace)::CPassant::NbCPassant'
I don’t get why. Here’s the code:
#include <iostream>
#include <unistd.h>
#include <vector>
#include "SleepChronogram.h"
#include "nsUtil.h"
using namespace std;
using namespace boost;
using namespace boost::posix_time;
using namespace nsUtil;
static unsigned CptThreads;
char * nsUtil::clrscr = "\033[2J"; // Clear screen
#define GOTOXY nsUtil::gotoxy
GOTOXY::gotoxy (int x, int y) : m_x (x), m_y (y) {}
ostream & nsUtil::operator << (ostream & os, const gotoxy & m)
{
return os << "\033[" << m.m_y << ';' << m.m_x << 'H';
}
#undef GOTOXY
namespace
{
void RendezVous ()
{
CptThreads--;
while(CptThreads)
{
condition_variable Cond;
mutex mut;
unique_lock<boost::mutex> lock(mut);
Cond.wait(lock);
}
}
class CPassant {
private:
static unsigned NbCPassant;
unsigned NbP;
unsigned TpsAvantRDV;
unsigned TpsApresRDV;
public:
CPassant (unsigned TpsAv, unsigned TpsAp)
:NbP(NbCPassant++) , TpsAvantRDV (TpsAv), TpsApresRDV (TpsAp) {}
void operator () ()
{
sleep(TpsAvantRDV);
RendezVous();
sleep(TpsApresRDV);
}
};
}
void nsUtil::SleepChronogram (unsigned Num,
unsigned & Col,
char Status,
unsigned Duree,
boost::mutex & ioMutex,
unsigned Decal /* = 10u */,
unsigned Periode /* = 1u */)
throw (std::ios_base::failure)
{
for (unsigned i = Duree; i--; )
{
{
lock_guard <mutex> Lock (ioMutex);
cout << gotoxy (Col++, Num + Decal) << Status << flush;
}
this_thread::sleep (seconds (Periode));
}
} // SleepChronogr()
int main (int argc, char ** argv)
{
if (argc != 1)
return 0;
int NbThreads = atoi(argv[1]);
if (NbThreads <1)
return 0;
vector <CPassant> VCPassant;
VCPassant.reserve(NbThreads);
for (int DelaiAv, DelaiAp, i (0) ; i < NbThreads; ++i)
{
cin >> DelaiAv >> DelaiAp;
VCPassant.push_back(CPassant(DelaiAv, DelaiAp));
}
return 0;
}
You are only declaring
NbCPassantin your code, you need to define it as well: