Note that there are some explanatory texts on larger screens.

plurals
  1. POnamespace and private static class members
    primarykey
    data
    text
    <p>Why does this work:</p> <pre><code>#include "iostream" class Something { private: static int s_nIDGenerator; int m_nID; friend int main(); public: Something() { m_nID = s_nIDGenerator++; } int GetID() const { return m_nID; } }; int Something::s_nIDGenerator; int main() { Something::s_nIDGenerator = 1; Something cFirst; Something cSecond; Something cThird; using namespace std; cout &lt;&lt; cFirst.GetID() &lt;&lt; endl; cout &lt;&lt; cSecond.GetID() &lt;&lt; endl; cout &lt;&lt; cThird.GetID() &lt;&lt; endl; return 0; } </code></pre> <p>it prints:</p> <pre><code>1 2 3 </code></pre> <p>And this fail:</p> <pre><code>#include "iostream" namespace test { class Something { private: static int s_nIDGenerator; int m_nID; friend int main(); public: Something() { m_nID = s_nIDGenerator++; } int GetID() const { return m_nID; } }; }; int test::Something::s_nIDGenerator; int main() { using namespace test; Something::s_nIDGenerator = 1; // or test::Something::s_nIDGenerator = 1; same effect if not using using. Something cFirst; Something cSecond; Something cThird; using namespace std; cout &lt;&lt; cFirst.GetID() &lt;&lt; endl; cout &lt;&lt; cSecond.GetID() &lt;&lt; endl; cout &lt;&lt; cThird.GetID() &lt;&lt; endl; return 0; } </code></pre> <p>With the compiler error message of:</p> <pre><code>**** Internal Builder is used for build **** g++ -O0 -g3 -Wall -c -fmessage-length=0 -o src\tuttest1.o ..\src\tuttest1.cpp ..\src\tuttest1.cpp: In function 'int main()': ..\src\tuttest1.cpp:23:5: error: 'int test::Something::s_nIDGenerator' is private ..\src\tuttest1.cpp:27:13: error: within this context Build error occurred, build is stopped Time consumed: 161 ms. </code></pre> <p>How do I get the 2nd example to work using the namespace test?</p> <p>How/Why is the namespace declaration around the object preventing the static member form being accessible?</p> <hr> <p>Per my comment to @zmo, here is what I got to work based on his clue:</p> <p>(comment doesn't have the space or formatting for this, and I had to edit because I couldn't set this an answer.... (what ever it takes.)</p> <pre><code>#include "iostream" namespace test { class Something { private: static int s_nIDGenerator; int m_nID; friend void load(int); public: Something() { m_nID = s_nIDGenerator++; } int GetID() const { return m_nID; } }; int Something::s_nIDGenerator; void load (int value) { Something::s_nIDGenerator = value; } }; int main() { using namespace test; load (1); Something cFirst; Something cSecond; Something cThird; using namespace std; cout &lt;&lt; cFirst.GetID() &lt;&lt; endl; cout &lt;&lt; cSecond.GetID() &lt;&lt; endl; cout &lt;&lt; cThird.GetID() &lt;&lt; endl; return 0; } </code></pre> <p>I am still a little loose as to the "what's up with static members being <strong><em>in a class and a namespace</em></strong> not working?" What's up with this? Why didn't <code>test::Something::s_nIDGenerator</code> work? (still a part of my original question.) So, we are half-answered, so far.</p> <p>I want to know <strong><em>why</em></strong> this didn't work so I don't walk into this rake again.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload