首页 > 其他 > 详细

使用muduo网络库编译出现error: reference to ‘_1’ is ambiguous

时间:2021-06-01 23:44:52      阅读:30      评论:0      收藏:0      [点我收藏+]

某程序使用了muduo网络库,而编译时报错:
技术分享图片
其实看编译报错原因就很明显了
std::placeholders::_1 和 boost库的extern const _Placeholder<1> _1 冲突了。

而用户代码并没有using namesapce std::placeholders
而是想使用boost::bind
如:

#include <muduo/net/TcpServer.h>
#include <muduo/net/EventLoop.h>
#include <muduo/net/InetAddress.h>

#include <boost/bind.hpp>

#include <stdio.h>

using namespace muduo;
using namespace muduo::net;

class TestServer
{
 public:
  TestServer(EventLoop* loop,
             const InetAddress& listenAddr)
    : loop_(loop),
      server_(loop, listenAddr, "TestServer")
  {
    server_.setConnectionCallback(
        boost::bind(&TestServer::onConnection, this, _1));
    server_.setMessageCallback(
        boost::bind(&TestServer::onMessage, this, _1, _2, _3));
  server_.setWriteCompleteCallback(
      boost::bind(&TestServer::onWriteComplete, this, _1));

原因是muduo库中使用了std::placeholders命名空间
在muduo/net文件夹下搜索
$ grep using ./*
可找到在Callbackes.h中
技术分享图片
如果在用户代码中真的要用boost::bind
可将其注释掉
技术分享图片
或者使用std::bind代替boost::bind

参考:
Why boost::bind insists pulling boost::placeholders into global namespace?

使用muduo网络库编译出现error: reference to ‘_1’ is ambiguous

原文:https://www.cnblogs.com/Lj-ming/p/14838751.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!