Please use boost/bind/bind.hpp + using namespace boost::placeholders

The practice of declaring the Bind placeholders (_1, _2, …) in the global namespace is deprecated. Please use <boost/bind/bind.hpp> + using namespace boost::placeholders, or define BOOST_BIND_GLOBAL_PLACEHOLDERS to retain the current behavior.

  • 提示warning的代码
#include <iostream>
#include <boost/bind.hpp>

using namespace std;
using namespace boost;

int fun(int x, int y) {return x+y;}
int main() {
    int m=1, n=2;
    cout << boost::bind(fun, _1, _2)(m, n) << endl;
    return 0;
}
  • 消除warning
#include <iostream>
#include <boost/bind/bind.hpp>

using namespace std;
using namespace boost::placeholders;

int fun(int x, int y) {return x+y;}
int main() {
    int m=1, n=2;
    cout << boost::bind(fun, _1, _2)(m, n) << endl;
    return 0;
}

关注我的公众号

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
给您提供一份修改后的代码: ```cpp #include "ros/ros.h" #include "std_msgs/String.h" #include <websocketpp/config/asio_client.hpp> #include <websocketpp/client.hpp> #include <websocketpp/common/thread.hpp> #include <boost/thread/locks.hpp> typedef websocketpp::client<websocketpp::config::asio_tls_client> client; class WebSocketClient { public: WebSocketClient() { // 设置websocket的回调函数 m_client.set_message_handler(std::bind(&WebSocketClient::on_message, this, std::placeholders::_1, std::placeholders::_2)); } void start() { // 连接到websocket服务器 websocketpp::lib::error_code ec; client::connection_ptr con = m_client.get_connection("wss://autopilot-test.t3go.cn:443/api/v1/vehicle/push/message/LFB1FV696M2L43840", ec); if (ec) { ROS_ERROR("连接失败: %s", ec.message().c_str()); return; } // 运行websocket的事件循环 m_client.init_asio(); m_client.start_perpetual(); m_client.connect(con); websocketpp::lib::thread asio_thread(&client::run, &m_client); asio_thread.join(); } void perceptionCallback(const std_msgs::String::ConstPtr& msg) { // 发送消息到websocket服务器 std::string serialized_data = msg->data; boost::unique_lock<boost::mutex> lock(m_mutex); m_send_queue.push(serialized_data); // 将消息加入发送队列 lock.unlock(); m_client.get_io_service().post(std::bind(&WebSocketClient::send_message, this)); // 通过io_service异步发送消息 } private: void send_message() { boost::unique_lock<boost::mutex> lock(m_mutex); if (m_send_queue.empty()) { return; } std::string message = m_send_queue.front(); m_send_queue.pop(); lock.unlock(); m_client.send(message, websocketpp::frame::opcode::text); // 发送消息 } void on_message(websocketpp::connection_hdl hdl, client::message_ptr msg) { // 处理websocket服务器返回的消息 ROS_INFO("收到消息: %s", msg->get_payload().c_str()); } client m_client; std::queue<std::string> m_send_queue; boost::mutex m_mutex; }; int main(int argc, char** argv) { ros::init(argc, argv, "websocket_client"); ros::NodeHandle nh; WebSocketClient ws_client; // 订阅感知话题 ros::Subscriber sub = nh.subscribe("perception_topic", 1000, &WebSocketClient::perceptionCallback, &ws_client); // 运行ROS事件循环 ros::spin(); return 0; } ``` 这份代码与之前的代码类似,但加入了一些新的功能。具体来说,它使用了一个发送队列和一个互斥锁来保证消息的线程安全,同时使用了`io_service`来异步发送消息。这样做可以避免阻塞ROS事件循环,提高程序的响应性能。另外,它还使用了ROS的日志功能,用于输出调试信息。请注意,这份代码需要安装websocketpp库才能编译通过。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值