对象要双重转换错误
Object to double conversion error
我试图通过超载双转换操作员来编写一个对象为大整数程序的双转换器。为此,我将大整数转换为一个字符串,然后使用STOD函数将其转换为double。这是函数的副本:
BigInteger::operator double () {
std::string *str = new std::string[this->m_digitCount];
for (int j = 0; j < this->m_digitCount; j++) {
str[j] = this->getDigit(j);
}
double a = std::stod(*str);
return a;
}
只是为了澄清m_digitCount是给定对象中的数字数。
当我运行该程序时,它会崩溃,并说'Abort()被调用。'
我插入了一个断点,发现该程序崩溃了
double a = std::stod(*str)
发生这种情况时,它说
indist4中的0x00007FFB1DEF7788的未经处理的异常 - big integer.exe:Microsoft C 例外:STD :: Invalid_argument在存储器位置0x0000000043f48fdd780。
有人可以帮我找到问题吗?非常感谢。
这不做您认为的事情:
std::string *str = new std::string[this->m_digitCount];
分配一系列空字符串。我不知道您的代码甚至是如何编译的。
无论如何,您的意思是:
std::string str;
str.reserve( m_digitCount );
for (int j = 0; j < m_digitCount; j++) {
str.push_back( getDigit(j) );
}
return std::stod(str);
请注意,数字应该是字符类型。如果仅是一个从0到9的整数,则必须执行str.push_back( '0' + getDigit(j) )
相关文章:
- C++使用params创建线程函数会导致转换错误
- 为什么g++在未执行的代码处标记强制转换错误
- 为什么C++在将浮点数转换为字符时没有显示缩小转换错误?
- 如何在构建对象堆栈时解决转换错误?
- 执行具有转换错误的过程
- 指向类成员函数的指针中存在类型转换错误
- 为什么我在这里收到C++没有已知的转换错误?
- 增加图形转换错误
- 尝试向 COM 对象添加另一个接口时出现静态强制转换错误 C2440
- 返回带有另一个类的数据成员的构造函数?遇到转换错误?
- 指针和双重转换错误,代码没有
- 自定义函数转换错误?
- 将 C 程序转换为具有大量字符* 和无符号字符转换错误的C++
- 如何在我使用过的Arduino库之一中固定构造函数中的转换错误
- 试图使用strCMP时的char转换错误
- 我在 TSP 的C++解决方案中遇到转换错误
- "double(*)(string, double, double)"尝试传递函数C++时"double"转换错误
- 此语法中的构造函数转换错误
- Opengl 精度转换错误编译错误 E0415
- 无效的转换错误和无效的类型