gpt4 book ai didi

c++ - 尝试返回 std::string 时出现 std::logic_error

转载 作者:太空宇宙 更新时间:2023-11-04 16:00:43 28 4
gpt4 key购买 nike

我已经使用一个类来编写一些代码来显示一个盒子的尺寸。我这样做是通过在 toString() 方法中输出,它似乎可以正常工作,但是当我运行该程序时,出现以下错误:

Height: 1 Width: 1 Depth: 1terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

然后程序崩溃了。我还注意到程序在 3 个维度后不显示音量。

代码如下:

#include <iostream>
#include <iomanip> // for output formatting
#include <stdexcept> // for out_of_range
#include <sstream> // for stringstream
#include <string>
#include <cstdlib> // for system()

using namespace std;

class Box
{

public:
// Constructors
Box(double height=1, double width=1, double depth=1);

// Mutators
void setHeight(double height);
void setWidth(double width);
void setDepth(double depth);

// Accessors
double getHeight() const {return (boxHeight);};
double getWidth() const {return (boxWidth);};
double getDepth() const {return (boxDepth);};
double getVolume() const;
string toString() ;
private:
double boxHeight;
double boxWidth;
double boxDepth;
double boxVolume;
};

int main()
{

cout << "\nBox Mesurement!";
cout << "\n===============";
cout << endl;

Box boxDem(true);

// WHERE THE STRING IS DISPLAYED
cout << "\n" << boxDem.toString();
cout<< endl;
cout << "\n" << boxDem.getVolume();

return 0;
}

Box::Box(double height, double width, double depth)
{
setHeight(height);
setWidth(width);
setDepth(depth);
}

void Box::setHeight(double height)
{
const double MIN = 0.01;

if (height > 0 && height < MIN)
{
height = 0.01;
boxHeight = height;
}
else if (height < 0)
{
height *= -1;
boxHeight = height;
}
else
{
boxHeight = height;
}
}
void Box::setWidth(double width)
{
const double MIN = 0.01;

if (width > 0 && width < MIN)
{
width = 0.01;
boxWidth = width;
}
else if (width < 0)
{
width *= -1;
boxWidth = width;
}
else
{
boxWidth = width;
}
}
void Box::setDepth(double depth)
{
const double MIN = 0.01;

if (depth > 0 && depth < MIN)
{
depth = 0.01;
boxDepth = depth;
}
else if (depth < 0)
{
depth *= -1;
boxDepth = depth;
}
else
{
boxDepth = depth;
}
}

double Box::getVolume() const
{
double volume = 0.0;

volume = getHeight() * getHeight() *getDepth();

return volume;

}

// WHERE THE PROBLEM IS
string Box::toString()
{
cout << "Height: " << getHeight() << " Width: " << getWidth() << " Depth: " << getDepth();
return 0;
}

最佳答案

cout 旨在将内容输出到命令行,但您正在编写一个应该返回 string 的函数,这毫无意义。

ostringstream 是一个简洁的类,它允许您使用与 cout 相同的机制构建字符串,试试这个:

string Box::toString() 
{
std::ostringstream result;
result << "Height: " << getHeight() << " Width: " << getWidth() << " Depth: " << getDepth();
return result.str();
}

关于c++ - 尝试返回 std::string 时出现 std::logic_error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45066946/

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