cpp: explanation about undefined reference to `(anonymous namespace)::CPassant::NbCPassant'
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/5603101/resolving-a-linker-error-undefined-reference-to-static-class-members">Resolving a linker error: undefined reference to static class members</a>  </p>
</blockquote>



<p>This declaration: <code>static unsigned NbCPassant;</code> only works when it's outside the class. If I put it inside, (to have it private) this doesn't compile, with the error:</p>

<pre><code>undefined reference to `(anonymous namespace)::CPassant::NbCPassant'
</code></pre>

<p>I don't get why. Here's the code:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;unistd.h&gt;
#include &lt;vector&gt;
#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 &amp; nsUtil::operator &lt;&lt; (ostream &amp; os, const gotoxy &amp; m)
{
    return os &lt;&lt; "\033[" &lt;&lt; m.m_y &lt;&lt; ';' &lt;&lt; m.m_x &lt;&lt; 'H';
}

#undef GOTOXY

namespace
{
    void RendezVous ()
    {
        CptThreads--;
        while(CptThreads)
        {
            condition_variable Cond;
            mutex mut;
            unique_lock&lt;boost::mutex&gt; 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 &amp; Col,
                              char Status, 
                              unsigned Duree,
                              boost::mutex &amp; ioMutex,
                              unsigned Decal   /* = 10u */,
                              unsigned Periode /* = 1u  */)
    throw (std::ios_base::failure)
{
    for (unsigned i = Duree; i--; )
    {
        {
            lock_guard &lt;mutex&gt; Lock (ioMutex);
            cout &lt;&lt; gotoxy (Col++, Num + Decal) &lt;&lt; Status &lt;&lt; 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 &lt;1)
        return 0;

    vector &lt;CPassant&gt; VCPassant;
    VCPassant.reserve(NbThreads);
    for (int DelaiAv, DelaiAp, i (0) ; i &lt; NbThreads; ++i)
    {
        cin &gt;&gt; DelaiAv &gt;&gt; DelaiAp;
        VCPassant.push_back(CPassant(DelaiAv, DelaiAp));
    }
    return 0;
}
</code></pre>
