添加 DIV/0 异常处理。我做错了什么,但无法确定在哪里
Add DIV/0 exception handling. I did something wrong but can't pin point where
这是一个简单的权重转换程序。目的是将英语重量1 5磅12盎司除以数字。 我使用 try-throw-catch 方法来处理除以 0。
请帮忙。
//Class Declaration
class EnglishWeight
{
private:
int pounds, ounces;
public:
EnglishWeight(void); //0-argument constructor
EnglishWeight(int lbs, int oz);
EnglishWeight ScalarDivision(const int &s, int *check) const;
};
//Member Function Definition
EnglishWeight::EnglishWeight(void)
{
pounds = ounces = 0;
}
EnglishWeight::EnglishWeight(int lbs, int oz)
{
pounds = lbs;
ounces = oz;
}
EnglishWeight EnglishWeight::ScalarDivision(const int &s, int *check) const //Scalar Division function
{
EnglishWeight sdivide; //Declare variable
try
{
if (s == 0) //DIV/0 check
throw 0;
else
{
sdivide.ounces = (pounds * 16) + ounces;
sdivide.pounds = sdivide.ounces / (16 * s);
sdivide.ounces = sdivide.ounces % 16;
return sdivide;
}
}
catch (int x)
{
*check = x;
}
}
void EnglishWeight::Show() const
{
std::cout << pounds << " pounds, " << ounces << " ounces";
}
//Main Driver
int main(void)
{
EnglishWeight EnglishWeight1(5, 12);
EnglishWeight sdiv1;
int number = 0;
int flag = 1;
sdiv1 = EnglishWeight1.ScalarDivision(number, &flag);
if (flag == 0)
std::cout << "EnglishWeight1 / number = DIV/0" << std::endl;
else
{
std::cout << "EnglishWeight1 / number = ";
sdiv1.Show();
std::cout << std::endl;
}
}
错误:0xC0000005:访问冲突读取位置0x0000000000000000这可能意味着编译器正在某处读取垃圾值。我只是不知道如何解决它。
@WhozCraig建议有效。谢谢!
"在发生div-0 异常的情况下,没有指定返回。因此,你基本上是在对EnglishWeight1.ScalarDivision的调用者撒谎,声称你会在所有代码路径上返回一个EnglishWeight。坦率地说,我无法理解您的编译器如何没有发出有关"并非所有代码路径都提供返回值"或类似措辞的某种警告。
"就按原样返回吧。无论如何,您的来电者都不会使用它">
相关文章:
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 我的C++线程做错了什么?
- 谁能告诉我我用 getline 做错了什么 (cpp) 格式
- 没有输出的合并排序我做错了什么?
- 我正在尝试使用 while 循环从字符串中删除字母,直到没有字母。我在这里做错了什么?
- 在C++中使用 AKS 素数测试计算双胞胎素数 我做错了什么?
- 我一直试图弄清楚我在这个链表程序中做错了什么
- 我正在尝试学习如何在 c++ 中传递指针,但出现错误:没有用于调用"test"的匹配函数。我做错了什么?
- FFMPEG,C++,内存泄漏,我做错了什么?
- 我做错了什么?反向字符串 C++
- 我在这个课上做错了什么?
- 创建整数的 2D 数组,该数组将使用两个函数用随机数填充矩阵.我做错了什么?
- 在不使用内置库函数的情况下添加字符串,我做错了什么?
- 我做错了什么?GetDiskFreeSpaceExA的功能根本不起作用
- 我在查找和字符串上做错了什么?
- 我正在尝试用 c++ 制作一个日志记录框架,但信息没有传递给记录器的子组件,我做错了什么?
- 我在尝试将多个值push_back向量时出错。我做错了什么?
- 插入排序:我做错了什么?