request for member 'toInt' in 'msg.std_msgs::String::data', which is of non-class type 'const _data_type {aka const char* const}'
时间: 2024-05-03 11:16:24 浏览: 16
这个错误提示是因为你试图在一个 `std_msgs::String` 类型的变量 `msg` 的 `data` 成员上调用 `toInt` 函数,但是 `data` 成员是一个 `const char*` 类型的指针,它并不是一个类类型,所以不能直接调用类成员函数 `toInt`。你需要先将 `data` 成员的值转换为整数类型,然后才能进行后续的操作。可以使用 `std::stoi` 函数将 `data` 转换为整数类型,例如:
```
int num = std::stoi(msg.data);
```
这样就可以将 `msg` 中的字符串类型的 `data` 成员转换为整数类型的 `num` 变量了。
相关问题
std_msgs::msg::string
std_msgs::msg::string是一个消息类型,它在ROS中被用于传递字符串数据。在C++代码中,我们可以创建一个std_msgs::String类型的消息对象,并使用msg.data来存储字符串数据。为了将数字与字符串拼接在一起,可以使用std::stringstream来实现。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [ROS学习笔记(二)](https://blog.csdn.net/weixin_49909631/article/details/121982119)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
没有与这些操作数匹配的 "=" 运算符C/C++(349) listener_str.cpp(12, 23): 操作数类型为: std::shared_ptr<rclcpp::Subscription<std_msgs::msg::String, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<std_msgs::msg::String, std::allocator<void>>>> = std::shared_ptr<rclcpp::Subscription<const std::shared_ptr<std_msgs::msg::String_<std::allocator<void>>> &, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<const std::shared_ptr<std_msgs::msg::String_<std::allocator<void>>> &, std::allocator<void>>>>
这个错误通常是因为尝试将一个类型为`std::shared_ptr<rclcpp::Subscription<const std::shared_ptr<std_msgs::msg::String_<std::allocator<void>>> &, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<const std::shared_ptr<std_msgs::msg::String_<std::allocator<void>>> &, std::allocator<void>>>>`的变量赋值给一个类型为`std::shared_ptr<rclcpp::Subscription<std_msgs::msg::String, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<std_msgs::msg::String, std::allocator<void>>>>`的变量,这两个类型虽然都是指向`rclcpp::Subscription`的`shared_ptr`,但是模板参数不同,无法直接赋值。
解决方法是将两个类型匹配,可以通过使用`std::static_pointer_cast`将其中一个类型转换成另一个类型,例如:
```
std::shared_ptr<rclcpp::Subscription<std_msgs::msg::String, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<std_msgs::msg::String, std::allocator<void>>>> sub;
std::shared_ptr<rclcpp::Subscription<const std::shared_ptr<std_msgs::msg::String_<std::allocator<void>>> &, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<const std::shared_ptr<std_msgs::msg::String_<std::allocator<void>>> &, std::allocator<void>>>> sub_const;
// 将 sub_const 转换成 sub 的类型
sub = std::static_pointer_cast<rclcpp::Subscription<std_msgs::msg::String, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<std_msgs::msg::String, std::allocator<void>>>>(sub_const);
```