How to access a public variable of subscriber class in the main function?
Hi there. I want to access "flow_message" (a public variable) which I defined in the subscriber class, in the main() function of my code. in the below code I just did a simple printing of the "flow_message" parameter to show the problem. the compilation has no error but when I run the subscriber node I get this error:
Segmentation fault (core dumped)
and this is the code:
#include<ros/ros.h>
#include<opencv_apps/FlowArrayStamped.h>
#include<opencv_apps/FlowArray.h>
#include<opencv_apps/Flow.h>
#include<opencv_apps/Point2D.h>
#include<vector>
#include<numeric>
using namespace std;
class listener
{
public:
vector<opencv_apps::Flow> flow_message;
void callback(const opencv_apps::FlowArrayStamped::ConstPtr& msg);
};
void listener::callback(const opencv_apps::FlowArrayStamped::ConstPtr& msg)
{
listener::flow_message = msg->flow;
ROS_INFO("i got it");
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "dataman");
ros::NodeHandle n;
listener list;
ros::Subscriber sub = n.subscribe("/lk_flow/flows", 1, &listener::callback, &list);
while(ros::ok())
{
cout << "this is it: " << list.flow_message[0] << endl;
ros::spinOnce();
}
return 0;
}
maybe my question would be so simple, but I searched and worker too much on it but I could not solve it. so thanks for any help...