Unable to use data received in callback function

asked 2019-05-01 14:05:04 -0500

elemecrobots gravatar image

updated 2019-05-01 14:15:59 -0500

gvdhoorn gravatar image

Hi. I am receiving data in a call back function using class. But i can't use data in main function. Any solutions?

#include "ros/ros.h"
#include "std_msgs/String.h"
#include "driving_msgs/StateArray.h"
#include "geometry_msgs/Twist.h"
#include <iostream>
using namespace std;

class server
{
  public:

    int totalobjects;
    void chatterCallback(const driving_msgs::StateArray& msg);
};

void server::chatterCallback(const driving_msgs::StateArray& msg)
 {
 totalobjects = msg.objects.size();
}


int main(int argc, char **argv)
{
  server objserver;
  ros::init(argc, argv, "listener");
  ros::NodeHandle n;
  ros::Subscriber sub = n.subscribe("/ground_truth", 1000, &server::chatterCallback, &objserver);
  ros::spin();
  cout << objserver.totalobjects;
  cout<<"\n";
  return 0;
}
edit retag flag offensive close merge delete

Comments

1