C++ 在C+中将双精度转换为char*/string+;
我很确定这不是一项艰巨的任务,但我不知道问题的原因,我想真正理解这一点,因为我经常遇到一些与指针/数组/强制转换相关的问题: 我将边界框值存储在双精度格式中*C++ 在C+中将双精度转换为char*/string+;,c++,string,pointers,double,C++,String,Pointers,Double,我很确定这不是一项艰巨的任务,但我不知道问题的原因,我想真正理解这一点,因为我经常遇到一些与指针/数组/强制转换相关的问题: 我将边界框值存储在双精度格式中* // this is the calss-variable double *_boundingBox; // this is where I put some data in it double boundingBox[6]; boundingBox[0] = . . . boundingBox[6] = ....; // set
// this is the calss-variable
double *_boundingBox;
// this is where I put some data in it
double boundingBox[6];
boundingBox[0] =
.
.
.
boundingBox[6] = ....;
// set pointer to boundingbox
_boundingBox = &boundingBox;
在另一节课上我用这个
double* getBoundingBoxInfo()
{
return _boundingBox;
}
要获取边界框数据,我希望将其作为QString输入QLabel中
double boundingBox[6];
boundingBox[0] = *_drawer->getBoundingBoxInfo();
std::string stringX = "x start: " << boundingBox[0] << "\tx end: " << boundingBox[3];
QLabel *labelX = new QLabel(QString(stringX.c_str()));
双边界框[6];
boundingBox[0]=*\u抽屉->getBoundingBoxInfo();
std::string stringX=“x start:”不能按原样将数据流化为std::string
。解决方案是使用std::ostringstream
:
std::ostringstream out;
out << "x start: " << boundingBox[0] << "\tx end: " << boundingBox[3];
std::string stringX = out.str();
std::ostringstream out;
out不能按原样将数据流化为std::string
。解决方案是使用std::ostringstream
:
std::ostringstream out;
out << "x start: " << boundingBox[0] << "\tx end: " << boundingBox[3];
std::string stringX = out.str();
std::ostringstream out;
out您得到的编译错误是针对“x开始:”您得到的编译错误是针对“x开始:”QString提供了一切
所以只要使用
QString("some text for double value: %1").arg(yourdouble, <additional parameters>)
QString提供一切
所以只要使用
QString("some text for double value: %1").arg(yourdouble, <additional parameters>)
我认为这只适用于c++11。我不认为它是在这个PCsI上运行的,我认为这只适用于c++11。我不认为它是在这台电脑上运行的。他们实际上是在试图将数据流转换成字符串文字。@JosephMansfield我知道,我是自愿选择“快捷方式”的。这是“正常”方式,还是我应该使用其他方式?@user2699453我想这很常见,可以认为它“正常”;这是一种将各种数据类型“转换”为字符串的简单方法(只要它们可以与一起使用就行了,但现在值是错误的:“boundingBox[0]=1.94655e-316 boundingBox[3]=8.29051e-316
”是getBoundingBoxInfo()之后的输出,“\u boundingBox[0]=0\u boundingBox[3]=1
”在他们试图将数据流转换成字符串文字之前。@JosephMansfield我知道,我是自愿选择“快捷方式”的。这是“正常”方式,还是我应该使用其他方式?@user2699453我想这已经足够普遍,可以认为是“正常的”;这是一种将各种数据类型“转换”为字符串的简单方式(只要它们可以与ok一起使用,这就行了,但现在值是错误的:“boundingBox[0]=1.94655e-316 boundingBox[3]=8.29051e-316
”是getBoundingBoxInfo()之后的输出,“\u boundingBox[0]=0\u boundingBox[3]=1
”在此之前,谢谢,事实上,我甚至在将我的行更改为\u boundingBox=&boundingBox[0];
之前都遇到了一个错误。如前所述,我不确定我是否应该使用指针和数组……如果编译器为\u boundingBox=&boundingBox
提供了一个错误,那么您可以执行\u boundingBox=(double*)boundingBox
(虽然,\u boundingBox=&boundingBox[0]
也很好)。谢谢,事实上我的行甚至出现了一个错误,直到我把它改为\u boundingBox=&boundingBox[0];
。正如前面写的,我不确定我是否应该使用指针和数组……你可以这样做\u boundingBox=(double*)boundingBox
如果编译器为\u boundingBox=&boundingBox
提供错误(但是,\u boundingBox=&boundingBox[0]
也可以)。