gpt4 book ai didi

c++ - 使用 static_cast 将 float 转换为字符串 c++

转载 作者:搜寻专家 更新时间:2023-10-30 23:58:15 25 4
gpt4 key购买 nike

所以这是我第一次使用 static_cast 进行“测试”,我从来没有这样做过,所以请耐心等待(我是 c++ 的新手,3 天前说过)

// ConsoleApplication3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
#include "string"


int main()
{
float value = 2.5f;
int temp;
std::cout << value;
std::cout << static_cast<std::string>(value) ;
std::cin.get();
}

报错

error C2440: 'static_cast' : cannot convert from 'float' to 'std::string'

IntelliSense: no suitable constructor exists to convert from "float" to "std::basic_string, std::allocator>"

我错过了什么吗?

最佳答案

static_cast 用于在彼此之间安全地转换可转换类型,这意味着共享相同基类的类型或在彼此之间定义转换运算符的类型。虽然 std::string 可以定义一个接受 float 的构造函数(不确定),但它不能从 float 转换。

关于c++ - 使用 static_cast<datatype> 将 float 转换为字符串 c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21193758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com