Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.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++_String_Floating Point - Fatal编程技术网

类型为';浮动';和';常数c'; 我刚开始学习C++,我需要一些帮助。

类型为';浮动';和';常数c'; 我刚开始学习C++,我需要一些帮助。,c++,string,floating-point,C++,String,Floating Point,targetDistance是一个float变量,我想给它添加一个字符串“a”,可以吗 我试过这个: targetDistance = targetDistance <<"a" 如果targetDistance是浮点,则需要将其转换为字符串,然后才能将其与另一个字符串连接。例如: auto result = std::to_string(targetDistance) + "a"; 其思想是将浮点变量(在本例中为targetDistan

targetDistance
是一个
float
变量,我想给它添加一个字符串
“a”
,可以吗

我试过这个:

  targetDistance = targetDistance <<"a"

如果targetDistance是浮点,则需要将其转换为字符串,然后才能将其与另一个字符串连接。例如:

auto result = std::to_string(targetDistance) + "a";

其思想是将浮点变量(在本例中为targetDistance)转换为字符串。 确保已包含此标题:

#include <string>
以下是它的简短版本:

string s = to_string( targetDistance ) + "a" ;

不,这是不可能的。我想知道你想在浮点数上加一个字符串是什么意思?这听起来像是一个误解。首先,你想让代码>目标距离>代码变成一个字符串吗?在这里有一些帮助学习C++ C++中的程序。就像在Python中,我会做代码>目标距离=目标距离+“A”/代码> @ StriZ:C++不同于Python。在Python中,对变量的赋值也会设置其类型。在C++中,类型不改变,因此必须匹配。(至少允许粗略地将int转换为float)请注意:
auto
告诉编译器猜测类型。在本例中,猜测了
std::string
,因此它相当于编写
std::string result=。在5分钟结束之前,获取此错误消息
“to_string”不是“std
@HolyBlackCat guess->推断”的成员?@strilz•
std::to_string
中声明,您需要处于C++11编译模式或更高版本(我的程序使用C++17编译模式)。如何指定编译器所编译的C++标准不同。@ HOLYBLCATH:猜想->演绎<代码>字符串< /COD> > >代码> STD::String < /代码>等。
string s; //to store our float variable
s= to_string( targetDistance ); //to_string function converts into string 
s= s+ "a";
string s = to_string( targetDistance ) + "a" ;