0

我是 C++ 的初学者,我正在将 C 代码(TWAMP RFC5357)移植到一个新项目(IPSLA RFC 6812)中,我遇到了这个问题:

当我尝试使用函数send时,我看到错误“error C2664: 'send' : cannot convert parameter 2 from 'ServerGreeting' to 'const char *'” “No user-defined-conversion operator available that can perform this conversion ,否则无法调用运算符”。

所以,我搜索了这个,我发现了一些我不明白的答案。

这是一些代码:

    typedef struct server_greeting
    {
       UINT8 未使用[12];
       UINT32 模式;
       UINT8 挑战[16];
       UINT8 盐[16];
       UINT32 计数;
       UINT8 MBZ[12];
    } 服务器问候;

struct active_session { int socket; // RequestSession req; }; struct client_info { int socket; sockaddr_in addr; int sess_no; struct active_session sessions[MAX_SESSIONS_PER_CLIENT]; timeval shutdown_time; }; static void cleanup_client (struct client_info *client) { fprintf (stderr,"Cleanup client %s\n", inet_ntoa(client->addr.sin_addr)); FD_CLR(client->socket,&read_fds); closesocket(client->socket); used_sockets--; int i; for (i = 0; i < client->sess_no; i++) /* If socket is -1 the session has already been closed */ if (client->sessions[i].socket > 0) { FD_CLR(client->sessions[i].socket, &read_fds); closesocket(client->sessions[i].socket); client->sessions[i].socket = -1; used_sockets--; } memset(client, 0, sizeof(struct client_info)); //client->status = kOffline; } static int send_greeting(UINT8 mode_mask, struct client_info *client) { int socket = client->socket; int i; ServerGreeting greet; memset(&greet, 0, sizeof(greet)); greet.Modes = 1 & mode_mask; for (i = 0; i < 16; i++) greet.Challenge[i] = rand() % 16; for (i = 0; i < 16; i++) greet.Salt[i] = rand() % 16; greet.Count = (1 << 12); int rv = send (socket, greet, sizeof(greet), 0); if (rv < 0) { fprintf(stderr, "[%s] ", inet_ntoa(client->addr.sin_addr)); perror("Failed to send ServerGreeting message"); cleanup_client(client); } else { printf("Sent ServerGreeting message to %s. Result %d\n", inet_ntoa(client->addr.sin_addr), rv); } return rv; }

你能帮我解决这个问题吗?谢谢

4

1 回答 1

1

send函数将指向缓冲区的指针作为其第二个参数const char*,因此您需要struct使用适当的强制转换传入您的地址,

int rv = send (socket, (const char*)&greet, sizeof(greet), 0);

编译器告诉您它无法执行从您尝试传递的结构到它期望的指针类型的转换,您应该相信它。

于 2015-05-10T15:38:48.063 回答