错误:从“const char *”到“char”的无效转换

时间:2012-11-28 17:50:01

标签: c++

这是linux btw ...

我从我的代码中收到此错误

m-server.cpp: In function âvoid* SocketHandler(void*)â:
m-server.cpp:187: error: invalid conversion from âcharâ to âconst void*â
m-server.cpp:187: error:   initializing argument 2 of âssize_t send(int, const void*, size_t, int)â

发出错误的部分:

char welcomemsg;

cout << "Set the welcome message: ";
cin >> welcomemsg;

//send initial welcome data || msg & src
send(*csock, welcomemsg, sizeof(welcomemsg), 0); //this is line 187

更新

我已经改变主意而不是cIN然后msg,我只想将其设置为下面的代码。

我从我的代码中收到此错误

m-server.cpp: In function âvoid* SocketHandler(void*)â:
m-server.cpp:181: error: invalid conversion from âconst char*â to âcharâ
m-server.cpp:230: error: jump to label âFINISHâ
m-server.cpp:177: error:   from here
m-server.cpp:181: error:   crosses initialization of âchar welcomemsgâ
m-server.cpp:230: error: jump to label âFINISHâ
m-server.cpp:161: error:   from here
m-server.cpp:181: error:   crosses initialization of âchar welcomemsgâ
m-server.cpp:230: error: jump to label âFINISHâ
m-server.cpp:149: error:   from here
m-server.cpp:181: error:   crosses initialization of âchar welcomemsgâ

发出错误的部分:

char welcomemsg = "@hello_! bro?"; //can allow symbols? 

//send initial welcome data || msg & src
send(*csock, (void *)&welcomemsg, sizeof(welcomemsg), 0);

我是c ++的新手,这里的问题是什么?怎么修?它背后的原因是什么?


更新

在我将其更改为此后,服务器没有任何错误。

char *welcomemsg = "hello";

//send initial welcome data || msg & src
send(*csock, welcomemsg, sizeof(welcomemsg), 0);

但是现在客户端在收到欢迎消息时崩溃了。为什么呢?
这是我与服务器一起使用的代码,客户端正在阅读欢迎消息。

send(*csock, "Hello_Word", 10, 0); //was a working code

更新

我已经检查过并且客户端在recv()上崩溃了 这段代码对所有建议有什么不同?为什么这个代码在没有崩溃的客户端时运行良好?

send(*csock, "Temporarily Offline_Server will open sockets on Jan. 14, 2013", 61, 0); //was a working code

更新

我的recv()代码:

response = "";
resp_leng= BUFFERSIZE;
while (resp_leng == BUFFERSIZE)
{
    resp_leng= recv(sock, (char*)&buffer, BUFFERSIZE, 0);
    if (resp_leng>0)
        response+= string(buffer).substr(0,resp_leng);
}

4 个答案:

答案 0 :(得分:2)

尽管在其他答案中有所陈述,但显式转换为[const] void *类型完全没有必要。但是,确实需要获取welcomemsg对象的地址

send(*csock, &welcomemsg, sizeof welcomemsg, 0);

以上将修复编译器错误。

但是,我猜这个问题实际上有不同的性质。您是否打算将“欢迎信息”仅包含一个字符?您的welcomemsg变量被声明为单个字符。如果你打算让它长一个字符,那么你应该把它声明为一个数组

char welcomemsg[100]; // for example

在这种情况下,&将变得不必要

send(*csock, welcomemsg, sizeof welcomemsg, 0);

但是,在这种情况下(如果您要发送字符串),您必须确定要发送的内容。您可以发送整个数组对象,即sizeof welcomemsg字节,如上例所示。或者您只能发送字符串的重要字符,即strlen(welcomemsg)字节(或strlen(welcomemsg) + 1字节),如

send(*csock, welcomemsg, strlen(welcomemsg), 0);

如果消息表示为

const char *welcomemsg = "some message";

你必须使用后一种变体。

但是当您使用运行时长度发送类似的字符串时,接收该字符串的问题会变得更加复杂。接收代码将如何知道要接收多少字节?通常,首先发送长度然后按实际字符串数据跟随它。

答案 1 :(得分:0)

send期望将void指针作为其第二个参数:send(int, const void*, size_t, int)

将您的代码更改为:

send(*csock, (void *)&welcomemsg, sizeof(welcomemsg), 0);

&表示法获取welcomemsg的地址。 (void *)部分不是必需的,但是它将地址从指向char的指针转换为void指针。


更新了问题:如果您要使用:

char welcomemsg = "hello";

//send initial welcome data || msg & src
send(*csock, (void *)&welcomemsg, sizeof(char) * 6, 0);

然后您需要将其更改为:

char *welcomemsg = "hello";

//send initial welcome data || msg & src
send(*csock, welcomemsg, strlen(welcomemsg), 0);

由于welcomemsg现在是指向字符数组开头的指针&#34; hello&#34;,因此已有地址。

答案 2 :(得分:0)

应为send(*csock, (void *)&welcomemsg, sizeof(welcomemsg), 0);

答案 3 :(得分:0)

让我谈谈我看到的一些错误。

char *welcomemsg = "hello";
send(*csock, welcomemsg, sizeof(welcomemsg), 0);

sizeof(welcomemsg)将返回指针的大小。我相信你实际上想要发送字符串,而不是指向它的指针(这在接收器端是没有意义的)。您可能希望将其设置为strlen(welcomemsg)+1。注意,发送字符串结尾字符需要+1!

send(*csock, "Hello_Word", 10, 0); //was a working code

如果有效,你很幸运。您想发送由{11}组成的{'H','e','l','l','o','_','W','o','r','d','\0'},而不是10个字节。 如果接收器在它之后已经为0,它可以正常工作,但其他任何东西都很可能会导致接收器端崩溃。

为了减轻计算不同字符串(文字与否)的痛苦,你可以实现一个简单的包装函数:

void sendString(int socket_descriptor, const char* str) {
    send(socket_descriptor, str, strlen(str)+1, 0);
}

只要str是一个正确的以null结尾的C字符串,这就应该有效。


我强烈建议您在尝试通过网络发送C字符串之前,先了解C字符串,了解它们如何工作,如何在函数之间传递它们等。