【问题标题】:Could not convert double to string无法将双精度转换为字符串
【发布时间】:2015-04-24 07:31:21
【问题描述】:

我一直在开发一个计算盒子体积的程序,使用类来帮助我理解如何使用类。作为程序的一部分,我想将对象的长度、宽度和高度转换为字符串以显示框的尺寸。当我从我的主文件运行代码时,它崩溃了。当我从类文件运行它时,我得到“无法将 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;
    }

【问题讨论】:

  • 什么?这是没有意义的。您无法从类文件中运行代码。 “无法将 Box::length 从 double 转换为 std::string”在您运行时不可能是错误消息。看起来像一个类型不匹配的编译器错误,它会说“无法将 Box::length 从 double 转换为 std::string”。
  • 不是运行,是编译看看没有错误
  • 不要将错误输出到值为strOut &lt;&lt; "Height is out of..."的字符串。这就是例外的原因。

标签: c++ dev-c++


【解决方案1】:

您的类定义说 getLength() 将返回一个字符串,但是您的 getLength() 函数实际上返回宽度,它是一个双精度值。您可能打算在返回之前将长度转换为字符串。

您可以使用字符串库中的 to_string() 函数,并将您的 getLength() 更改为

return to_string(length)

【讨论】:

  • 我更改了返回语句,我是否也应该更改原始声明,因为它说“to_string 未在此范围内声明”。
  • 你应该在顶部插入#include ,因为to_string在字符串库中
  • 另外,只是一点建议:为了将来参考,getWidth() 应该返回一个双精度值,因为宽度是双精度值。在您的代码中保持一定的一致性是一个很好的做法。您应该定义一个新函数 getWidthAsString() 来返回字符串,或者在主函数本身中使用 to_string 将 getWidth() 返回值转换为字符串。
  • 等等,没关系。您可以通过使用 -std=c++11 选项进行编译来修复错误,因为 to_string 在 C++11 之前不可用。
【解决方案2】:

此编译错误发生在 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

【讨论】:

  • 我将函数声明更改为字符串 getWidth();等仍然给出“无法转换”
  • 仔细看其实有一堆小错误。例如,stringstream 与 string 不同,所以 toString 应该返回strOut.str()。此外,您所有的 getter 函数,包括 getVolume() 都应该是 const。我只是以这种方式编译并运行代码。
  • 另外,您是否将函数声明和函数定义都更改为相同的返回类型?如,文件顶部是否有double getWidth(); 和后来的double Box::getWidth() { return width }。它们应该匹配。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-20
  • 2020-12-14
  • 2020-07-17
  • 2011-08-11
  • 1970-01-01
相关资源
最近更新 更多