[SOLVED] How to subscribe a std_msgs::msg::String and copy in a std::string?
Hello I'm having some issues in subscribing a topic containing std_msgs::msg::string
on ros2 galactic. I have defined a std::string
and want to subscribe the topic and do something like `string=msg.data. Here my code:
#include <memory>
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include "kdl_parser/kdl_parser.hpp"
using std::placeholders::_1;
class ROBOT_INVDYN : public rclcpp::Node{
public:
ROBOT_INVDYN() : Node("robot_invdyn"){
subscription_ = this->create_subscription<std_msgs::msg::String> ("robot_description", 10, std::bind(&ROBOT_INVDYN::topic_callback,this, _1));
}
private:
std::string robot;
void topic_callback(const std_msgs::msg::String &msg)const{
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg.data.c_str());
robot=msg.data;
}
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
};
int main(int argc, char * argv[]){
rclcpp::init(argc, argv);
auto robot_node = std::make_shared<ROBOT_INVDYN>();
rclcpp::spin(robot_node);
rclcpp::shutdown();
return 0;
}
but I keep on receiving errors on the operator =
as:
error: no match for ‘operator=’ (operand types are ‘const string’ {aka ‘const std::__cxx11::basic_string<char>’} and ‘const _data_type’ {aka ‘const std::__cxx11::basic_string<char>’})
17 | robot_desc_string = msg.data;
| ^~~~
Could you help me?
Please share your complete code
Yes, sure I will edit.