Can't create constructor that creates a subscriber node inside
I am trying to program in C++ to get the GPS data from "/mavros/global_position/global"
. The code right now ( This is a header file ):
#pragma once
#include <sensor_msgs/NavSatFix.h>
#include <ros/ros.h>
class Localization{
public:
// Functions
void receiver(const sensor_msgs::NavSatFixConstPtr&);
// Constructors
Localization(ros::NodeHandle);
private:
// Data types
double latitiude;
double longitude;
double altitude;
const std::string topic {"/mavros/global_position/global"};
};
void Localization::receiver(const sensor_msgs::NavSatFixConstPtr& fix){
if (fix->status.status == sensor_msgs::NavSatStatus::STATUS_NO_FIX) {
ROS_INFO("No fix.");
return;
}
latitiude = (*fix).latitude;
longitude = (*fix).longitude;
altitude = (*fix).altitude;
ROS_INFO("Latitude: %f | Longitude: %f | Altitude: %f", latitiude, longitude, altitude);
};
Localization::Localization(ros::NodeHandle nh){
ros::Subscriber location = nh.subscribe(topic, 1, receiver);
};
However, when I tried to build it with catkin_make
. I receive the following error:
error: invalid use of non-static member function ‘void Localization::receiver(const NavSatFixConstPtr&)’
ros::Subscriber location = nh.subscribe(topic, 1, receiver);
^
/home/kannachan/drone/src/quantum_drone/scripts/missions/gps.h:11:9: note: declared here
void receiver(const sensor_msgs::NavSatFixConstPtr& fix){