Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在c++;_C++ - Fatal编程技术网

C++ 如何在c++;

C++ 如何在c++;,c++,C++,我尝试使用一个函数将long转换为const char*,因为代码中的函数socket->write需要一个数据类型为const char*data的参数。但是如果我查看我的输出,convertedLabel什么都不是,我不知道为什么。我想我没有正确的转换函数 我的代码: void ServerNet::sendData(long long label270) { std::cout << "First:" << std::endl;

我尝试使用一个函数将
long
转换为
const char*
,因为代码中的函数
socket->write
需要一个数据类型为
const char*data
的参数。但是如果我查看我的输出,
convertedLabel
什么都不是,我不知道为什么。我想我没有正确的转换函数

我的代码:

void ServerNet::sendData(long long label270) {
    std::cout << "First:" << std::endl;
    std::cout << label270 << std::endl;

    //Error is here:
    const char* convertedLabel = reinterpret_cast<char * const>(label270);

    std::cout << "Then:" << std::endl;
    std::cout << convertedLabel << std::endl;
    socket->write(convertedLabel);
}
#包括
#包括
void SendData(常量字符*数据){

std::cout和你们的人一起,tipps我处理了这个问题。下面是解决方案:

void ServerNet::sendData(long long label270) {
    std::string convertedLabel = std::to_string(label270);
    const char *test = convertedLabel.c_str();
    socket->write(test);
}

谢谢大家!

如果它看起来像“什么都没有”,那么它就是指向文本
'\0'
(可能是偶然的)。你打算在电线上发送什么?@Coop4Free如果你需要这样的施法,这意味着你做错了。@Ext3h有一个很好的观点。你是想用文本格式还是二进制格式编写
long-long
?值
0
只是一个字节,还是
sizeof(long-long)
协议中的字节数?如果是后者,为什么有线协议的规格如此不明确?你不能依赖于
sizeof(long lon)==sizeof(std::int64\t)
@VladfromMoscow或仅仅使用C API。那行标记为“Error is here”确实是一个错误。<代码> const char */<代码>和代码> char * const 是两种不同类型;该代码< > RealTytCase<代码>的结果不能被分配给<代码>转换标签>代码>。看起来AKK0RD77正确地猜测了您的意图。您应该对其答案进行投票,并将其标记为接受。
#include <iostream>
#include <string>

void SendData(const char* Data) {
    std::cout << Data << std::endl;
}

int main() {
    long long label270 = 2173457687;
    SendData(std::to_string(label270).c_str());
    return 0;
}
void ServerNet::sendData(long long label270) {
    std::string convertedLabel = std::to_string(label270);
    const char *test = convertedLabel.c_str();
    socket->write(test);
}