title: error: invalid use of non-static member function
body: <h2>Issue:</h2>
  
  <p>I am trying to instantiate a class (say class A) inside another class (say class B) and then call all the member functions of class A inside the class B for the instantiated object. I get the following error message for every call of class A methods inside class B: </p>
  
  <blockquote>
    <p>error: invalid use of non-static member function</p>
  </blockquote>
  
  <p>I am aware of the following thread about the same problem. <a href="https://stackoverflow.com/questions/41326376/invalid-use-of-non-static-member-function-c">Invalid use of non-static member function c++</a>. But I don't find that solution relevant to my problem. I am elaborating the problem below. Can you please enlighten me what is going wrong ?</p>
  
  <h2>Code:</h2>
  
  <p>The following are my classes:</p>
  
  <p><strong>Navigation.h</strong></p>
  
  <pre><code>class Navigation {
  public:
      Navigation() {
      laserData = n.subscribe("/scan", 200, &amp;Navigation::laserCallback, this);
      velPub = n.advertise&lt;geometry_msgs::Twist&gt; ("/mobile_base/commands/velocity", 100, this);
  }
      ~Navigation() {}
      geometry_msgs::Twist moveCommand();
      geometry_msgs::Twist turnCommand();
      geometry_msgs::Twist stopCommand();
      void laserCallback(const sensor_msgs::LaserScan::ConstPtr&amp; data);
      float getObstacleRange();
      void broadcastVelocity(geometry_msgs::Twist velocity);
  
  private:
      ros::NodeHandle n;
      ros::Publisher velPub;
      ros::Subscriber laserData;
      float obstacleRange;
      ros::Timer normalTurnTimer;
      ros::Timer driveTimer;
      ros::Timer periodicTurnTimer;
      geometry_msgs::Twist drivePower;
  };
  </code></pre>
  
  <p><strong>Navigation.cpp</strong></p>
  
  <pre><code>geometry_msgs::Twist Navigation::moveCommand() {
      return drivePower;
  }
  
  geometry_msgs::Twist Navigation::turnCommand() {
      return drivePower;
  }
  
  geometry_msgs::Twist Navigation::stopCommand() {
      return drivePower;
  }
  
  void Navigation::laserCallback(const sensor_msgs::LaserScan::ConstPtr&amp; data) {
      obstacleRange = 0.8;
  
  }
  
  float Navigation::getObstacleRange() {
      return obstacleRange;
  }
  
  void Navigation::broadcastVelocity(geometry_msgs::Twist velocity) {
      velPub.publish(velocity);
  }
  </code></pre>
  
  <p><strong>Turtlebot.h</strong></p>
  
  <pre><code>class Turtlebot {
  public:
      Turtlebot() {
          Navigation nomad;
      }
      ~Turtlebot() {}
      void drive();
  
  private:
      Navigation nomad;
  };
  </code></pre>
  
  <p><strong>Turtlebot.cpp</strong></p>
  
  <pre><code>void Turtlebot::drive() {
      if (nomad.getObstacleRange() &lt; 0.7) {
          nomad.broadcastVelocity(nomad.stopCommand);
          int i;
          while (i &lt; 5) {
              nomad.broadcastVelocity(nomad.turnCommand);
              i++;
          }
      } else nomad.broadcastVelocity(nomad.moveCommand);
  }
  </code></pre>
  
  <h2>Errors:</h2>
  
  <p>I get the following errors during <strong><em>cmake .. &amp;&amp; make</em></strong></p>
  
  <pre><code>/home/arun/Documents/catkin_ws/src/TinyNomad/src/Turtlebot.cpp: In member function ‘void Turtlebot::drive()’:
  /home/arun/Documents/catkin_ws/src/TinyNomad/src/Turtlebot.cpp:37:50: error: invalid use of non-static member function
       nomad.broadcastVelocity(nomad.stopCommand);
                                                ^
  /home/arun/Documents/catkin_ws/src/TinyNomad/src/Turtlebot.cpp:40:54: error: invalid use of non-static member function
           nomad.broadcastVelocity(nomad.turnCommand);
                                                    ^
  /home/arun/Documents/catkin_ws/src/TinyNomad/src/Turtlebot.cpp:43:53: error: invalid use of non-static member function
   } else nomad.broadcastVelocity(nomad.moveCommand);
                                                   ^
  TinyNomad/CMakeFiles/tinynomad.dir/build.make:110: recipe for target 'TinyNomad/CMakeFiles/tinynomad.dir/src/Turtlebot.cpp.o' failed
  make[2]: *** [TinyNomad/CMakeFiles/tinynomad.dir/src/Turtlebot.cpp.o] Error 1
  CMakeFiles/Makefile2:1191: recipe for target 'TinyNomad/CMakeFiles/tinynomad.dir/all' failed
  make[1]: *** [TinyNomad/CMakeFiles/tinynomad.dir/all] Error 2
  Makefile:138: recipe for target 'all' failed
  make: *** [all] Error 2
  Invoking "make -j12 -l12" failed
  </code></pre>