簡體   English   中英

c++ using namespace std 導致 bind () 錯誤和迭代器放棄

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

我添加時遇到問題

using namespace std;

編譯器說沒有任何糖果可以渴望綁定。

簡單的例子:

#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;
}

現在會很簡單

using namespace std;

放棄......但我還有更多:

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

當我刪除 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 

我不知道在哪里放另一個 std::

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

不是在迭代器之前...

不要使用using namespace std; . 您遇到了它導致的主要問題:

  • 在名為bindstd命名空間中存在一個 function 。
  • WinSock2 Sockets 庫中存在一個名為bind的 function 。

通過包含using namespace std ,編譯器不知道該選擇哪一個。

不管是否使用using namespace std ,都會強制您通過std::bind(...)::bind(...)消除調用歧義

您使用std::invoke得到的錯誤是一個單獨的問題。 您是否包括<functional> header?


至於這個:

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

使用autorange-based-for-loop

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM