0

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:

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::vector<Structs*>::iterator it = m_ClientList.begin();

not before iterator...

  • 3
    Best practice is to not include `using namespace std;`. https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice Put together a [mcve] that demonstrates the error you're receiving. – Retired Ninja Mar 18 '21 at 16:26

1 Answers1

3

Don't use using namespace std;. You've run into the main problem it causes:

  • There exists a function in the std namespace called bind.
  • There exists a function in the WinSock2 Sockets library called bind.

By including using namespace std, the compiler does not know which one to pick.

Regardless, using using namespace std or not, forces you to disambiguate the calls via std::bind(...) and ::bind(...)

The error you are getting with std::invoke is a separate issue. Are you including the <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:

for(auto& client : m_ClientList) {
    //...
}
Casey
  • 10,297
  • 11
  • 59
  • 88
  • 1
    Even with `using namespace std;`, you can still disambiguate the calls by using `::bind()` to call the Winsock function in the global namespace: `rc = ::bind(acceptSocket, (SOCKADDR*)&addr, sizeof(SOCKADDR_IN));` – Remy Lebeau Mar 18 '21 at 16:47
  • @RemyLebeau Whups. Typo. Fixed. – Casey Mar 18 '21 at 16:48
  • Thank you. I had already understood the problem with the ambiguity. but thanks for solving the iterator problem. For the sake of interest, I would be interested in the correct syntax for std :: vector :: iterator it = m_ClientList.begin (); without using the using namespace std ... –  Mar 20 '21 at 06:40