错误C2664:'发送' :无法从' ServerGreeting'转换参数2到#char; char *'

时间:2015-05-10 15:23:26

标签: c++ struct c2664

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

当我尝试使用发送功能时,我看到错误"错误C2664:'发送' :无法从' ServerGreeting'转换参数2 to' const char *'" "没有可用于执行此转换的用户定义转换运算符,或者无法调用运算符"。

所以,我搜索了这个,我找到了一些我不理解的答案。

以下是一些代码:

    typedef struct server_greeting
    {
       UINT8 Unused[12]; 
       UINT32 Modes;
       UINT8 Challenge[16];
       UINT8 Salt[16];
       UINT32 Count; 
       UINT8 MBZ[12];
    } ServerGreeting;

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; }

你能帮帮我吗?感谢

1 个答案:

答案 0 :(得分:1)

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

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

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