0

Hello I am using Ubuntu 18.04 and I am very new to Ros. I was using ros kinetic. I have switched to the Ros melodic version and I am getting a compilation error in my cpp file. Below you can see my code and error I will be very happy if you could help. this is my code:

    #include <ros/ros.h>
    #include <std_msgs/String.h>
    #include <move_base_msgs/MoveBaseAction.h>
    #include <actionlib/client/simple_action_client.h>
    #include <iostream>
    #include <string.h>
    
    using namespace std;
    
    /** Function Declarations **/
    bool moveToGoal(double xGoal, double yGoal, double yaw);
    void choose();
    
    //code*****
    
    
    
    void move(const std_msgs::String msg){
        cout << "move Function activated" << endl;
        //***code
    }
    
    int main(int argc, char** argv){
        ros::init(argc, argv, "map_navigation4");
        ros::NodeHandle n;
    
        ros::Subscriber sub = n.subscribe ("server_messages", 1, move);
    
        ros::spin();
        return 0;
    }
    
    bool moveToGoal(double xGoal, double yGoal, double yaw){
        //**code
    }

and this error message: enter image description here

  • 1
    Why are you using `using namespace std;` and defining a function named `move`? See [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Jason Jun 15 '22 at 12:49
  • This isn't the OP's problem but I landed here with the same error message. In my case the error was to declare the `ros::NodeHandle` `const`. – Roland Sarrazin Jan 11 '23 at 12:23

1 Answers1

0

It looks like you subscriber is not recognizing your move callback function. Below modification to your code should fix the issue-

int main(int argc, char** argv){
    ros::init(argc, argv, "map_navigation4");
    ros::NodeHandle n;

    ros::Subscriber sub = n.subscribe("server_messages", 1, moveCB);

    ros::spin();
    return 0;
}

And the callback function -

void moveCB(const std_msgs::String::ConstPtr &msg)
{
     cout << "move Function activated" << endl;
     const char *a = msg->data.c_str();
     /**
     * Your Code
     */
}

Reference

  1. ROS code documentation
  2. ROS Publisher and Subscriber Tutorial
  • Thank you for your attention and assistance. I tried editing it but the error could not be resolved. Maybe I don't understand exactly what you're saying. I am also pretty bad at using cpp. Could there be definitions about libraries that I need to change? – SuleKayiran May 06 '21 at 11:26
  • Hi, Sorry for the delay. When run `ros::spin()` it will start the subscriber thread `moveCB`. This function will start every time `server_messages` topic is updated. – Vishnuprasad Prachandabhanu May 17 '21 at 08:43
  • Your callback function should be unique so that it recognizes that topic. Have tried change the name of your callback function? – Vishnuprasad Prachandabhanu May 17 '21 at 08:50
  • thank you for your attention @VishnuprasadPrachandabhanu I've tried and didn't work. i think the problem is with ros version because the cod seamless for ros kinetic. I'm still searching. But I went back to ros kinetic to prevent my project from slowing down. – SuleKayiran May 20 '21 at 05:45