“无法推断模板参数”将 Winsock bind() 调用与 Boost 结合使用时出错

发布于 2024-08-26 00:11:43 字数 1339 浏览 5 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尹雨沫 2024-09-02 00:11:43

有一个名为bind()的Boost函数,这是一个完全不同的东西来自Winsock 的bind()

如果您需要在给定模块中使用这两个功能,您有两个选择:

  1. 不要说“使用命名空间提升”。相反,在代码中明确限定 Boost 的使用。我更喜欢这个选项,因为 Boost 是一个第三方库,它的短名称可能会与其他第三方库以及采用 Boost 功能的 C++ 的未来版本发生冲突。有时,如果单个函数包含 Boost 的多种用途,我会在其中说“使用命名空间 boost”。

  2. 显式限定全局 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:

  1. 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.

  2. Explicitly qualify the use of the global Winsock bind():

    ::bind(mLocalSocketFd, ...

物价感观 2024-09-02 00:11:43

正如您从 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 to std::tr1::bind.

So, probably you're using namespace std;.

As @Warren Young indicated, ::bind will direct your call to the unqualified bind identifier - which then is WinSock's in your case.

太傻旳人生 2024-09-02 00:11:43

我刚刚遇到了同样的问题,并在这里找到了微软的官方答案: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 中的所有名称都可用于非限定的
名称查找。因此,Winsock 的bind() 和
在重载决策期间会考虑bind(),并且因为
的bind()是一个模板,它通常会获胜,但会失败
稍后编译。

解决方案

要解决此问题,请在需要 Winsock 的 bind() 时调用 ::bind(),并且
std::bind() 如果你想要 的bind()。当你打电话时
::bind(),您要求编译器在全局命名空间中查找
仅此而已。

或者,不要使用命名空间 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, so using namespace std acts like using namespace boost for the bind() function with VS2010.

If you ever have anything call using namespace std in advance then

you make all of the names in namespace std available to unqualified
name lookup. Therefore, both Winsock's bind() and 's
bind() are considered during overload resolution, and because
's bind() is a template, it will usually win, but fail to
compile later.

Solution:

To fix this, call ::bind() when you want Winsock's bind(), and
std::bind() if you ever want 's bind(). When you call
::bind(), you're asking the compiler to look in the global namespace
only.

Or alternatively, don't use namespace std.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文