无法将double转换为字符串
Could not convert double to string
我一直在开发一个计算盒子体积的程序,使用类来帮助我了解如何使用类。作为程序的一部分,我想将对象的长度、宽度和高度转换为字符串,以显示框的尺寸。当我从主文件运行代码时,它会崩溃。当我从类文件中运行它时,我得到"无法将Box::length从double转换为std::string。如何修复转换错误?
#include <iostream>
#include <stdexcept>
#include <sstream>
#include <iomanip>
#include <string>
using namespace std;
class Box
{
public:
double length;//length of the box
double height;//height of the box
double width;//with of the box
Box(): length(1), height(1), width(1){}
//Parameterized Constructor
Box(double length, double width, double height);
double getVolume(void);
//Mutators
void setLength(double leng);
void setWidth(double wid);
void setHeight(double hei);
//Acessors
string toString() const;
string getLength();
string getWidth();
string getHeight();
};//end class
//member function definitions
double Box::getVolume(void)//get volume will cal and output the volume when called
{
return length * width * height;
}
void Box::setLength(double leng)
{
const double MIN_LENGTH = 0.1;//constants for min/max for range check and out_of_range exception
const double MAX_LENGTH = 99;
if (length > MAX_LENGTH || length < MIN_LENGTH)
{
stringstream strOut;//declare string stream
strOut << "Length is out of range. Length must be between" << MIN_LENGTH << " and " << MAX_LENGTH << ".";//error msg
throw out_of_range(strOut.str());
}
else
{
length = leng;// if length is within range, store it
}
}
string Box::getLength()
{
return length;
}
void Box::setWidth(double wid)
{
const double MIN_WIDTH = 0.1;//constants for min/max for range check and out_of_range exception
const double MAX_WIDTH = 99;
if (length > MAX_WIDTH || length < MIN_WIDTH)
{
stringstream strOut;//declare string stream
strOut << "Width is out of range. Width must be between" << MIN_WIDTH << " and " << MAX_WIDTH << ".";//error msg
throw out_of_range(strOut.str());
}
else
{
width = wid;// width is in range, store it
}
}
string Box::getWidth()
{
return width;
}
void Box::setHeight(double hei)
{
const double MIN_HEIGHT = 0.1;//constants for min/max for range check and out_of_range exception
const double MAX_HEIGHT = 99;
if (length > MAX_HEIGHT || length < MIN_HEIGHT)
{
stringstream strOut;//declare string stream
strOut << "Height is out of range. Height must be between" << MIN_HEIGHT << " and " << MAX_HEIGHT << ".";//error msg
throw out_of_range(strOut.str());
}
else
{
height = hei;// height is in range, store it
}
}
string Box::getHeight()
{
return height;
}
string Box::toString() const
{
stringstream strOut;
strOut << "Length: " << getLength() << endl
<< "Width: " << getWidth() << endl <<
"Height: " << getHeight() << endl;
return strOut;
}
此编译错误发生在getWidth、getHeight和getLength函数的返回语句中。这是因为您声明它们返回一个字符串,但却返回了一个双倍的宽度、高度和长度。编译器发现没有从double到string的自动转换。
要解决此问题,您只需要将函数的返回类型从字符串固定为双精度:
double getLength();
double getWidth();
double getHeight();
我注意到另一个错误:
doubletostring.cpp:104:25: error: reference to non-static member function must be called
strOut << "Length: " << getLength << endl
以及toString方法中的类似错误。只需在末尾添加()
即可将它们转换为函数调用:
strOut << "Length: " << getLength() << endl
您的类定义说getLength()将返回一个字符串,但您的getLength(。您可能想在返回之前将长度转换为字符串。
您可以使用字符串库中的to_string()函数,并将getLength()更改为
return to_string(length)
相关文章:
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 当字符串存储在变量中时,如何将字符串转换为wchar_t
- 如何将这个std::字符串转换为std::基本字符串
- 如何正确将字符串转换为标准::时间::system_clock::time_point?
- 如何在C++中将十六进制字符串转换为文本数据
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- 如何从字符串转换为双精度*
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")
- 如何在C++中将 Python 字符串转换为其转义版本?
- 如何在 C++11 中将标准::字符串转换为标准::u32字符串?
- 如何将整数字符串转换为整数的二维向量?
- 使用 "stringstream " 将字符串转换为整数
- 将字符串转换为浮点数或整数,而无需使用内置函数(如 atoi 或 atof)
- 将文件复制到自定义位置,存在字符串转换问题
- 将ANSI C字符串转换为UNICODE
- 如何将节点 V8 字符串转换为 C++ 字符串
- 在C++中将字符串转换为双精度,而无需科学记数法
- 使用从文件(stod、strtod、atof)中提取的数据C++从字符串转换为双精度.csv
- 如何在C++中将字符串转换为双精度?
- 将字符串转换为无符号字符数组/字节数组