简体   繁体   English

c++ using namespace std 导致 bind () 错误和迭代器放弃

[英]c++ using namespace std causes errors with bind () and a waiver with the iterator

I have the problem when I add我添加时遇到问题

using namespace std;

that the compiler says that there is no confection to long for bind.编译器说没有任何糖果可以渴望绑定。

simple example:简单的例子:

#include <winsock2.h>
#include <mutex>
#include <iostream>

using namespace std;

int main()
{
    WSADATA wsa;
    long rc = WSAStartup(MAKEWORD(2, 0), &wsa);
    SOCKADDR_IN addr;
    SOCKET acceptSocket = socket(AF_INET, SOCK_STREAM, 0);


    memset(&addr, 0, sizeof(SOCKADDR_IN));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(1234);
    addr.sin_addr.s_addr = ADDR_ANY;
    rc = bind(acceptSocket, (SOCKADDR*)&addr, sizeof(SOCKADDR_IN));

    return 0;
}

it would be a simple one now现在会很简单

using namespace std;

to renounce... but I have further:放弃......但我还有更多:

    std::vector<Structs*>::iterator it = m_ClientList.begin();
    for (; it != m_ClientList.end(); ++it)

and when i remove the using namespace std I get the following error message:当我删除 using namespace std 时,我收到以下错误消息:

C2672 "std :: invoke": no matching overloaded function found 
C2893 Function template "unknown-type std :: invoke (_Callable &&, _ Types && ...) noexcept (<expr>)" could not be specialized 

I don't know where to put another std:: in我不知道在哪里放另一个 std::

std::vector<Structs*>::iterator it = m_ClientList.begin();

not before iterator...不是在迭代器之前...

Don't use using namespace std;不要使用using namespace std; . . You've run into the main problem it causes:您遇到了它导致的主要问题:

  • There exists a function in the std namespace called bind .在名为bindstd命名空间中存在一个 function 。
  • There exists a function in the WinSock2 Sockets library called bind . WinSock2 Sockets 库中存在一个名为bind的 function 。

By including using namespace std , the compiler does not know which one to pick.通过包含using namespace std ,编译器不知道该选择哪一个。

Regardless, using using namespace std or not, forces you to disambiguate the calls via std::bind(...) and ::bind(...)不管是否使用using namespace std ,都会强制您通过std::bind(...)::bind(...)消除调用歧义

The error you are getting with std::invoke is a separate issue.您使用std::invoke得到的错误是一个单独的问题。 Are you including the <functional> header?您是否包括<functional> header?


As for this:至于这个:

std::vector<Structs*>::iterator it = m_ClientList.begin();
for (; it != m_ClientList.end(); ++it)

Use auto and a range-based-for-loop :使用autorange-based-for-loop

for(auto& client : m_ClientList) {
    //...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM