错误:从 "const char*" 到 "char" 的转换无效
error: invalid conversion from "const char*" to "char"
这是在Linux上...
我从我的代码中收到此错误
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);
}
尽管其他答案中说明了什么,但显式转换为[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";
您将不得不使用后一种变体。
但是,当您发送这样的字符串时,使用运行时长度,接收该字符串的问题变得更加复杂。接收代码如何知道要接收多少字节?通常,人们会先发送长度,然后是实际的字符串数据。
这应该是send(*csock, (void *)&welcomemsg, sizeof(welcomemsg), 0);
send
期待一个空指针作为它的第二个参数: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
现在是指向字符数组"hello"开头的指针,因此已经有一个地址。
让我解决我看到的一些错误。
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 个字节而不是 10 个字节组成的{'H','e','l','l','o','_','W','o','r','d',' '}
。 如果接收器之后已经为 0,则它工作正常,但其他任何事情都可能导致接收器端崩溃。
为了减轻计算不同字符串(文字或非文字)的痛苦,您可以实现一个简单的包装器函数:
void sendString(int socket_descriptor, const char* str) {
send(socket_descriptor, str, strlen(str)+1, 0);
}
只要str
是适当的以 null 结尾的 C 字符串,这应该有效。
我强烈建议你稍微玩一下C字符串,了解它们是如何工作的,如何在函数之间传递它们,等等,然后再尝试通过网络发送它们。
- 将无符号char*转换为std::istream*C++
- 为什么这个函数将"const char*"转换为"void* const"而不是"const void*"
- 将函数参数"const char*"转换为"std::string_view"是
- C ++如何在使用"tolower"时将char转换为int
- 有没有办法在C++的赋值中将"char**"转换为"double"?
- JavaCPP 错误:无法将参数"1"的"char*"转换为"Abc*
- 为什么从 char 转换为 std::byte 可能是未定义的行为?
- 如何在虚幻引擎4中将char*转换为TCHAR?
- C++:将模板类型 char 转换为 std::string
- strcmp/char* 转换无法按预期工作
- C++中的SQLite char*转换
- 无法从 const char* 转换为 const char *&
- 在 C++ 中将 char 转换为 int,而无需符号位传播
- 如果必须包含西里尔字符,如何将 char* 转换为 std::string?
- 试图使用strCMP时的char转换错误
- 无法从'const char *'转换为'LPCTSTR'
- 将 std::vector<char> 转换为字符* 会导致字符缺陷
- 使用 static_cast 将 char 转换为 int c++
- 如何在 C++(MFC) 中将 char* 转换为 LPCTSTR
- 无法将const char转换为字符串构造字符串