Venelin
Venelin

Reputation: 3308

C++ - No matching function for call to 'getaddrinfo'

Here is my code:

const char portNum = TCP_PORT;
const unsigned int backLog = 8;  // number of connections allowed on the incoming queue


addrinfo hints, *res, *p;    // we need 2 pointers, res to hold and p to iterate over
memset(&hints, 0, sizeof(hints));

// for more explanation, man socket
hints.ai_family   = AF_UNSPEC;    // don't specify which IP version to use yet
hints.ai_socktype = SOCK_STREAM;  // SOCK_STREAM refers to TCP, SOCK_DGRAM will be?
hints.ai_flags    = AI_PASSIVE;


// man getaddrinfo
int gAddRes = getaddrinfo(NULL, portNum, &hints, &res);
if (gAddRes != 0) {
    std::cerr << gai_strerror(gAddRes) << "\n";
 

In Define.h i have:

#define TCP_PORT 8080

With the code like that I get an error passing partNum in getaddrinfo.

Here is what getaddrinfo requires:

extern int getaddrinfo(const char *__name, const char *__service, const addrinfo *__req, addrinfo *__pai)

I am pretty new to C++ so excuse me if my question seems stupid to you, but why I get an error No matching function for call to 'getaddrinfo'.

The second parameter should be a pointer? Am I getting this correctly?

If so how can I fix it actually?

Upvotes: 1

Views: 409

Answers (1)

Adrian Mole
Adrian Mole

Reputation: 51825

The getaddrinfo() function requires a const char *service second argument. If this 'service' is a port number, then it should be specified in string form. Thus, you should define your TCP_PORT macro as follows:

#define TCP_PORT "8080"

Then, you will also need to change the type of the portnum variable to be a pointer (to a constant character string) rather than (as you currently have it) a single character:

const char *portNum = TCP_PORT; // "portnum" now is the string literal address.

Your call to getaddrinfo should now work.

Upvotes: 1

Related Questions