std::logic_error 尝试返回 std::string 时
std::logic_error when trying to return a std::string
我使用一个显示盒子测量值的类编写了一些代码。我通过将输出放在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();
}
相关文章:
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 如何获取std::result_of函数的返回类型
- 架构决策:返回std::future还是提供回调
- 如果我std::dynamic_pointer_cast并且底层dynamic_cast的结果为null,那么返回的sh
- 使用std::mt19937从字符串中返回一个随机单词
- 如何使用std::min和std::less返回对象
- <Windows>为什么 std::thread::native_handle 返回类型为"long long unsigned int"的值,而不是 void*(又名 HANDLE)?
- 返回一个带有 std::move 的对象并链接函数
- 推导 std::vector::back() 的返回类型
- 为什么 C++ std::unordered_map 从 emplace/ 找到返回一个迭代器?
- 如何从 std::thread 返回值
- 从 std<Derived>::shared_ptr 的向量返回 std::shared_ptr<Base>
- 在带有尾部斜杠的路径上返回 std::filesystem::create_directories() 的值
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- 如何从 std::vector 搜索和返回项目
- 从嵌套循环中的 std::list 中删除将返回访问冲突
- 我无法使用C++指针指向类方法返回的 std::vector
- 如何使用 uint64_t 键类型从 std::map<int, std::string> 返回值?
- 使用 std::p air 进行返回值优化
- 将 std::map::emplace 与返回 shared_ptr 的函数一起使用是否正确?