undefined references to a static class member (C++)

2 posts / 0 new
Last post
salass00
salass00's picture
Offline
Last seen: 7 months 2 weeks ago
Joined: 2011-02-03 11:27
undefined references to a static class member (C++)

I've written a simple RNG class in C++ for use in a project:

  1. /* Copyright (c) 2012 Fredrik Wikstrom */
  2.  
  3. #include "types.h"
  4. #include <cassert>
  5.  
  6. namespace Game {
  7.  
  8. typedef uint32_t random_T;
  9. typedef int32_t srandom_T;
  10.  
  11. const int RNG_J = 31;
  12. const int RNG_K = 55;
  13. const random_T RNG_MAX = 0xffffffff;
  14.  
  15. class RNG_State {
  16. public:
  17. random_T v[RNG_K+1];
  18. random_T x, j, k;
  19. bool init;
  20.  
  21. RNG_State() : init(false) { }
  22. };
  23.  
  24. class RNG {
  25. private:
  26. static RNG_State m_state;
  27. public:
  28. static void Seed(random_T seed) {
  29. int i;
  30. m_state.v[0] = seed;
  31. for (i = 1; i <= RNG_K; i++) {
  32. m_state.v[i] = 3 * m_state.v[i-1] + 257;
  33. }
  34. m_state.x = RNG_K;
  35. m_state.j = RNG_K-RNG_J;
  36. m_state.k = 0;
  37. m_state.init = true;
  38. }
  39.  
  40. static random_T Rand() {
  41. random_T rand;
  42. assert(m_state.init);
  43. rand = m_state.v[m_state.j] * m_state.v[m_state.k];
  44. m_state.x = (m_state.x + 1) % (RNG_K+1);
  45. m_state.j = (m_state.j + 1) % (RNG_K+1);
  46. m_state.k = (m_state.k + 1) % (RNG_K+1);
  47. m_state.v[m_state.x] = rand;
  48. return rand;
  49. }
  50.  
  51. static random_T RandMax(random_T max) {
  52. assert(max > 0);
  53. return Rand() / (RNG_MAX / (max + 1));
  54. }
  55.  
  56. static srandom_T RandRange(srandom_T min, srandom_T max) {
  57. assert(max > min);
  58. return min + RandMax(max - min);
  59. }
  60.  
  61. static random_T RandChance(random_T a, random_T b) {
  62. assert(b > 0);
  63. return RandMax(b - 1) < a;
  64. }
  65. };
  66.  
  67. }

It's supposed to be usable without having an object so all methods are declared as static and the one structure that is used to store state data is declared as a static too but still when I compile and link my code it generates undefined references to the m_state member:


$ make
g++ -O2 -Wall -std=c++0x -Iinclude -c -o main.o main.cpp
g++ -o game main.o -lcurses
main.o: In function `main':
main.cpp:(.text.startup+0xa0): undefined reference to `Game::RNG::m_state'
main.cpp:(.text.startup+0xab): undefined reference to `Game::RNG::m_state'
main.cpp:(.text.startup+0xb9): undefined reference to `Game::RNG::m_state'
main.cpp:(.text.startup+0xcc): undefined reference to `Game::RNG::m_state'
main.cpp:(.text.startup+0xd6): undefined reference to `Game::RNG::m_state'
main.o:main.cpp:(.text.startup+0xe0): more undefined references to `Game::RNG::m_state' follow
collect2: ld returned 1 exit status
make: *** [game] Error 1

Maybe someone who is more experienced than me with C++ can explain what is going on here and how I should fix this?

salass00
salass00's picture
Offline
Last seen: 7 months 2 weeks ago
Joined: 2011-02-03 11:27
Found the solution after

Found the solution after googling around for a bit. Apparently I have to create a .cpp file with the actual variable definition in it and then compile and link it to my program for this to work.

  1. /* Copyright (c) 2012 Fredrik Wikstrom */
  2.  
  3. #include "rng.h"
  4.  
  5. namespace Game {
  6.  
  7. RNG_State RNG::m_state;
  8.  
  9. };
Log in or register to post comments