title: C++ static member functions and variables
body: <p>I am learning C++ by making a small robot simulation and I'm having trouble with static member functions inside classes.</p>
  
  <p>I have my Environment class defined like this:</p>
  
  <pre><code>class Environment {
      private:
          int numOfRobots;
          int numOfObstacles;
  
          static void display(); // Displays all initialized objects on the screen
  
      public:
          Robot *robots;
          Obstacle *obstacles;
  
          // constructor
          Environment();
  
          static void processKeySpecialUp(int, int, int); // Processes the keyboard events
  };
  </code></pre>
  
  <p>Then in the constructor I initialize the robots and obstacles like this:</p>
  
  <pre><code>numOfRobots = 1; // How many robots to draw
  numOfObstacles = 1;
  robots = new Robot[numOfRobots];
  obstacles = new Obstacle[numOfObstacles];
  </code></pre>
  
  <p>Here is example of static function that uses those variables:</p>
  
  <pre><code>void Environment::display(void) {
      // Draw all robots
      for (int i=0; i&lt;numOfRobots; i++) {
          robots[i].draw();
      }
  }
  </code></pre>
  
  <p>When I try to compile, I get error messages like</p>
  
  <pre><code>error: invalid use of member ‘Environment::robots’ in static member function
  </code></pre>
  
  <p>I tried making numOfRobots, numOfObstacles, robots and obstacles <strong>static</strong>, but then I got errors like</p>
  
  <pre><code>error: undefined reference to 'Environment::numOfRobots'
  </code></pre>
  
  <p>I would greatly appreciate of someone could explain me what I am doing wrong.
  Thank you!</p>