2

I'm trying to make a client list for a UDP Server.

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include "stdafx.h"
#include <map>
#include <unordered_map>
#include <WS2tcpip.h>
#include <WinSock2.h>
#include <Windows.h>

#pragma comment(lib, "Ws2_32.lib")


std::map<sockaddr_in, int> clientList;

....
void Listen(){
    sockaddr_in client;
    [...]
    if (clientList.find(client) == clientList.end()) {
        printf("Got a new client!\n");
        std::pair<sockaddr_in, uint64_t> item(client, rand());
        clientList.insert(item);
    }
}

I get the following error:

C2678 binary '<': no operator found which takes a left-hand operand of type 'const sockaddr_in'

I also tried using unordered_map which gives me the following error:

C2280 'std::hash<_Kty>::hash(const std::hash<_Kty> &)' :attempting to reference a deleted function

It compiles fine when I only declare the map. I can't seem to figure out what's wrong since sockaddr_in is defined by WinSock2. I'm using VS2017.

David
  • 71
  • 10
  • 4
    May be this can help [std::maps with user-defined types as key](https://stackoverflow.com/questions/1102392/stdmaps-with-user-defined-types-as-key) – Mohammad Kanan Jun 05 '18 at 23:38
  • 2
    Maps need a comparison function since it's a binary search tree, and unordered maps need a hash function for the has table. – eesiraed Jun 06 '18 at 00:30
  • 2
    To use `sockaddr_in` as a `std::map` key, you need an overload of `operator<` or a specialization of `std::less()` for `sockaddr_in`, or provide the `map` template with a custom `Compare` function. To use `sockaddr_in` as a `std::unordered_map` key, you need a specialization of `std::hash()` for `sockaddr_in`, or provide the `underdered_map` template with custom `Hash` and `KeyEqual` functions. – Remy Lebeau Jun 06 '18 at 01:58

0 Answers0