“无法推断模板参数”将 Winsock bind() 调用与 Boost 结合使用时出错
我对 C++ 还很陌生,可能有点不知所措,但事情就是这样。
我正在处理一个相当大的 Win32 C++ 项目,该项目使用 Winsock 进行网络通信。我正在尝试将其线程管理的一堆转换为 boost,但是一旦我添加了对 boost 库的引用以及其他什么,我在这个特定的代码行上收到了大约 30 个错误
bind(mLocalSocketFd, (struct sockaddr *) &localServerAddress, sizeof(localServerAddress));
:错误包括以下内容:
error C2602: 'std::tr1::_Result_of2<_Fty,_Farg0,_Farg1>::_Type' is not a member of a base class of 'std::tr1::_Result_of2<_Fty,_Farg0,_Farg1>'
error C2868: 'std::tr1::_Result_of2<_Fty,_Farg0,_Farg1>::_Type' : illegal syntax for using-declaration; expected qualified-name
error C2784: 'bool std::operator <(const std::vector<_Ty,_Ax> &,const std::vector<_Ty,_Ax> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'std::tr1::_Bind_fty<_Fty,_Ret,_BindN>'
error C2784: 'bool std::operator <(const std::vector<_Ty,_Ax> &,const std::vector<_Ty,_Ax> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'std::tr1::_Bind_fty<_Fty,_Ret,_BindN>'
我认为我已经设法告诉这个特定文件(“NetServer.cpp”)使用bind()的boost版本,但对于我的生活,我无法弄清楚它在哪里正在发生。我使用的 boost 的唯一部分是 boost/thread.hpp,并且我没有在 NetServer.cpp 或其链接到的头文件中的任何位置使用任何命名空间。
关于我做错了什么或者如何解决问题有什么建议吗?这显然是一些愚蠢的新手事情,但我无法弄清楚。
I'm fairly new to C++, and I'm likely in over my head, but that's the way it goes.
I'm working with a fairly large Win32 C++ project that uses Winsock for its network communications. I'm in the process of trying to convert a bunch of its thread management over to boost, but once I added the references to the boost libraries and what-not, I'm getting about 30 errors on this particular line of code:
bind(mLocalSocketFd, (struct sockaddr *) &localServerAddress, sizeof(localServerAddress));
The errors include things like:
error C2602: 'std::tr1::_Result_of2<_Fty,_Farg0,_Farg1>::_Type' is not a member of a base class of 'std::tr1::_Result_of2<_Fty,_Farg0,_Farg1>'
error C2868: 'std::tr1::_Result_of2<_Fty,_Farg0,_Farg1>::_Type' : illegal syntax for using-declaration; expected qualified-name
error C2784: 'bool std::operator <(const std::vector<_Ty,_Ax> &,const std::vector<_Ty,_Ax> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'std::tr1::_Bind_fty<_Fty,_Ret,_BindN>'
error C2784: 'bool std::operator <(const std::vector<_Ty,_Ax> &,const std::vector<_Ty,_Ax> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'std::tr1::_Bind_fty<_Fty,_Ret,_BindN>'
I presume that somehow I've managed to tell this particular file ("NetServer.cpp") to use the boost version of bind(), but for the life of me, I can't figure out where that's happening. The only portion of boost that I'm using is the boost/thread.hpp, and I'm not doing any namespace using's anywhere in NetServer.cpp or in the header files that it links to.
Any suggestions as to what I'm doing wrong, or how to troubleshoot it? It's obviously some stupid newbie thing, but I can't figure it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个名为bind()的Boost函数,这是一个完全不同的东西来自Winsock 的bind()。
如果您需要在给定模块中使用这两个功能,您有两个选择:
不要说“使用命名空间提升”。相反,在代码中明确限定 Boost 的使用。我更喜欢这个选项,因为 Boost 是一个第三方库,它的短名称可能会与其他第三方库以及采用 Boost 功能的 C++ 的未来版本发生冲突。有时,如果单个函数包含 Boost 的多种用途,我会在其中说“使用命名空间 boost”。
显式限定全局 Winsock bind() 的使用:
::bind(mLocalSocketFd, ...
There's a Boost function called bind() which is a totally different thing from Winsock's bind().
You have two options if you need both functions available in a given module:
Don't say "using namespace boost". Instead, explicitly qualify uses of Boost in your code. I prefer this option since Boost is a third-party library and its short names may conflict both with other third-party libraries and with future versions of C++ that adopt Boost features. Occasionally I will say "using namespace boost" within a single function if it contains several uses of Boost.
Explicitly qualify the use of the global Winsock bind():
::bind(mLocalSocketFd, ...
正如您从 MSVC 的神秘错误消息中看到的,您的
bind
调用将转到std::tr1::bind
。因此,您可能正在使用
namespace std;
。正如 @Warren Young 所指出的,
::bind
会将您的调用定向到非限定的bind
标识符 - 在您的情况下,该标识符就是WinSock
的标识符。As you can see from MSVC's cryptic error message, your
bind
call goes tostd::tr1::bind
.So, probably you're using
namespace std;
.As @Warren Young indicated,
::bind
will direct your call to the unqualifiedbind
identifier - which then isWinSock
's in your case.我刚刚遇到了同样的问题,并在这里找到了微软的官方答案:http://connect.microsoft.com/VisualStudio/feedback/details/500364/how-to-avoid-conflicts- Between-tr1-bind- and-winsock-bind-function
简短版本:即使您没有调用 boost 命名空间,我猜您在某处有一个
using namespace std
,因为我猜测您使用的是 VS2010,它具有 tr1 扩展名,因此using namespace std
的作用类似于 VS2010 中的 bind() 函数的using namespace boost
。如果您有任何事情请提前调用
using namespace std
解决方案:
或者,不要使用命名空间 std。
I've just encountered the same issue, and found an official Microsoft answer here: http://connect.microsoft.com/VisualStudio/feedback/details/500364/how-to-avoid-conflicts-between-tr1-bind-and-winsock-bind-function
Short version: Even if you are not calling boost namespace, I guess you have a
using namespace std
somewhere, and since I guess you are using VS2010, it has the tr1 extension, sousing namespace std
acts likeusing namespace boost
for the bind() function with VS2010.If you ever have anything call
using namespace std
in advance thenSolution:
Or alternatively, don't use namespace std.