无法从“std::_Binder std::_Unforced,SOCKET &,LPSOCKADDR,unsigned int ”转换为“int”

本文探讨了在C++11中使用std::bind时与Socket类中的bind函数发生名称冲突的问题。详细介绍了错误产生的原因及解决方案,即避免在使用socket.h的类中使用using namespace std。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

无法从“std::_Binder<std::_Unforced,SOCKET &,LPSOCKADDR,unsigned int>”转换为“int”

使用Socket类时遇到函数冲突的问题,情况是这样的,Socket中有一个bind函数, 原型如下 : 

     if (bind(server_fd,(struct sockaddr*)&server_addr, sizeof(struct sockaddr)) == -1){
        LOG4CPLUS_DEBUG(logger, __FILE__ << __LINE__ << "绑定套字接失败");
        return -1;
    }

错误提示

错误 1 error C2678: 二进制“==”: 没有找到接受“std::_Bind<false,void,SOCKET &,sockaddr *,unsigned int>”类型的左操作数的运算符(或没有可接受的转换)    c:\users\administrator\desktop\wechatserver\network.cpp    414    1    WechatServer
 2    IntelliSense:  没有与这些操作数匹配的 "==" 运算符
   操作数类型为: std::_Bind<false, void, SOCKET &,sockaddr *, unsigned int> == int

   c:\Users\Administrator\Desktop\WechatServer\Network.cpp    414

原因是在C++11中增加了std::bind(_Fp &&__f, _BoundArgs &&__bound_args...) 函数, 可以用该函数绑定函数指针, 当在C++类中添加了using namespace std;之后,如果你想调用socket.h中的bind方法则会出现问题, 使得bind函数调用不是你想要的行为。解决方案是在使用socket.h的bind函数的类中不要使用using namespace std。

void CameraDeviceSession::updateBufferCaches(const hidl_vec&lt;BufferCache&gt;&amp; cachesToRemove) { 1. lock_acquire: 调用 Autolock 将获取锁 this-&gt;mInflightLock.mMutex。[显示详情] 1107 Mutex::Autolock _l(mInflightLock); 2. 对 cachesToRemove 的另一元素迭代。 5. 对 cachesToRemove 的另一元素迭代。 1108 for (auto&amp; cache : cachesToRemove) { 1109 auto cbsIt = mCirculatingBuffers.find(cache.streamId); 3. 条件 cbsIt == std::__1::map&lt;int, std::__1::unordered_map&lt;unsigned long long, native_handle const *, std::__1::hash&lt;unsigned long long&gt;, std::__1::equal_to&lt;unsigned long long&gt;, std::__1::allocator&lt;std::__1::pair&lt;unsigned long long const, native_handle const *&gt; &gt; &gt;, std::__1::less&lt;int&gt;, std::__1::allocator&lt;std::__1::pair&lt;int const, std::__1::unordered_map&lt;unsigned long long, native_handle const *, std::__1::hash&lt;unsigned long long&gt;, std::__1::equal_to&lt;unsigned long long&gt;, std::__1::allocator&lt;std::__1::pair&lt;unsigned long long const, native_handle const *&gt; &gt; &gt; &gt; &gt; &gt;::iterator const(this-&gt;mCirculatingBuffers.end()),使用了 true 分支。 6. 条件 cbsIt == std::__1::map&lt;int, std::__1::unordered_map&lt;unsigned long long, native_handle const *, std::__1::hash&lt;unsigned long long&gt;, std::__1::equal_to&lt;unsigned long long&gt;, std::__1::allocator&lt;std::__1::pair&lt;unsigned long long const, native_handle const *&gt; &gt; &gt;, std::__1::less&lt;int&gt;, std::__1::allocator&lt;std::__1::pair&lt;int const, std::__1::unordered_map&lt;unsigned long long, native_handle const *, std::__1::hash&lt;unsigned long long&gt;, std::__1::equal_to&lt;unsigned long long&gt;, std::__1::allocator&lt;std::__1::pair&lt;unsigned long long const, native_handle const *&gt; &gt; &gt; &gt; &gt; &gt;::iterator const(this-&gt;mCirculatingBuffers.end()),使用了 false 分支。 1110 if (cbsIt == mCirculatingBuffers.end()) { 1111 // The stream could have been removed 4. 继续循环 1112 continue; 1113 } 1114 CirculatingBuffers&amp; cbs = cbsIt-&gt;second; 1115 auto it = cbs.find(cache.bufferId); 7. 条件 it != std::__1::unordered_map&lt;unsigned long long, native_handle const *, std::__1::hash&lt;unsigned long long&gt;, std::__1::equal_to&lt;unsigned long long&gt;, std::__1::allocator&lt;std::__1::pair&lt;unsigned long long const, native_handle const *&gt; &gt; &gt;::iterator const(cbs-&gt;end()),使用了 true 分支。 1116 if (it != cbs.end()) { CID 171518: (#1 of 1): 持有锁时等待 (SLEEP) 8. sleep: 对 freeBuffer 的调用可能在持有锁 this-&gt;mInflightLock.mMutex 的情况下睡眠。[显示详情] 1117 sHandleImporter.freeBuffer(it-&gt;second); 1118 cbs.erase(it); 1119 } else { 1120 ALOGE(&quot;%s: stream %d buffer %&quot; PRIu64 &quot; is not cached&quot;, 1121 __FUNCTION__, cache.streamId, cache.bufferId); 1122 } 1123 } 1124
最新发布
07-27
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值