user3663882
user3663882

Reputation: 7367

Compiler cannot parse a function call

I have the code that uses recv function:

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

char *buf = new char[1000];

int main()
{
        SOCKET ConnectSocket;
        addrinfo hints, *result;
        do{
                result = recv(ConnectSocket, buf, 1000, result);
                result = recv(ConnectSocket, buf, 1000, 0);
                std::cout << result << std::endl;
        } while (result > 0);
}

I'm going to run that code on windows 8, cygwin. The issue is when I copile that code with g++ -c mmm.cpp I got the following error:

$ g++ -c mmm.cpp
mmm.cpp: In function ‘int main()’:
mmm.cpp:15:49: error: invalid conversion from ‘addrinfo*’ to ‘int’ [-fpermissive]
   result = recv(ConnectSocket, buf, 1000, result);
                                                 ^
In file included from mmm.cpp:2:0:
/usr/include/w32api/winsock2.h:992:34: note: initializing argument 4 of ‘int recv(SOCKET, char*, int, int)’
   WINSOCK_API_LINKAGE int WSAAPI recv(SOCKET s,char *buf,int len,int flags);
                                  ^
mmm.cpp:15:49: error: invalid conversion from ‘int’ to ‘addrinfo*’ [-fpermissive]
   result = recv(ConnectSocket, buf, 1000, result);
                                                 ^
mmm.cpp:16:44: error: invalid conversion from ‘int’ to ‘addrinfo*’ [-fpermissive]
   result = recv(ConnectSocket, buf, 1000, 0);

What the hell does it mean? First, the compiler told us that it couldn't convert addrinfo* to int and then it told that it couldn't perform reverse convertion. Does the function recv(SOCKET, char*, int, int) exist at all?

Upvotes: 0

Views: 104

Answers (1)

user0815
user0815

Reputation: 1406

The function you are calling is defined as follows:

int recv(
  _In_   SOCKET s,
  _Out_  char *buf,
  _In_   int len,
  _In_   int flags
);

In the first case you are passing a variable of type addrinfo* as 4. argument. This can not be converted to int as required by the function definition.

In the second and third case, you assign the return value which is of type int to a variable of type addrinfo* and this causes another problem.

Upvotes: 5

Related Questions