gpt4 book ai didi

c++ - 在构造函数定义中调用函数后收到 Segmentation Fault 11 错误?

转载 作者:行者123 更新时间:2023-11-28 06:18:39 40 4
gpt4 key购买 nike

我研究过这个错误,它似乎在危险的内存分配或过载中很常见,但我找不到适用于我的代码的地方。基本上我一直收到错误消息:

Segmentation fault: 11

每次我在参数化分数构造函数定义中调用我的简化函数后运行我的代码。有趣的是,当我在这个定义中调用 change 函数时,它被完全忽略了。我试图弄清楚如何让改变至少起作用,这样我就可以弄清楚如何正确应用简化,但我已经这样做了很多年我仍然不知所措。

我有三个文件:fraction.h、fraction.cpp 和 main.cpp。 Main.cpp 是由我的教授编写的——这是创建头文件和实现文件以允许他的代码运行的作业。这个特定部分的说明是:

A private member function called simplify that reduces the fraction to its lowest terms (12/15 => 4/5). If you write the function, you should call the simplify function within the parameterized constructor definition, so that the function is created in simplified form.

//分数.h :

#ifndef FRACTION_
#define FRACTION_

#include <stdio.h>
#include <iostream>
#include <math.h>

using namespace std;

class Fraction {

private:
int numerator;
int denominator;
int change (int a, int b);
Fraction simplify (int n, int d);

public:

Fraction();

Fraction (int num, int denom);

Fraction sumWith (Fraction a);

Fraction multiplyWith (Fraction z);

void const print (ostream & out) const;

};

#endif /* defined(____Fraction__) */

//fraction.cpp [不包括未受影响的功能]:

#include "fraction.h"
#include <iostream>
#include <math.h>

using namespace std;

int Fraction::change(int a, int b){
int temp;
temp = a;
a = b;
b = temp;
return temp; }

Fraction Fraction::simplify (int n, int d){
int r, gcd, true_n, true_d, q;

true_n = n;
true_d = d;

if ((n) < (d)){
change (n, d);
q = 5; }

r = (n % d);

if ((r != 1) && (r != 0)){
for (int i = 0; r > 1; i++){
r = (n % d);
d = n;
r = d;
i++; }

if (r == 0){
gcd = n;
n = true_n/gcd;
d = true_d/gcd; }

else {
n = true_n;
d = true_d; }
}

if (r == 0){
n = n/d;
d = 1; }

if (q == 5){
change (n, d); }

return Fraction(n, d); }

Fraction::Fraction() {
numerator = 1;
denominator = 1; }


Fraction::Fraction (int num, int denom) {

if (denom == 0) {
cerr << "Denominator may not be 0.";
exit (EXIT_FAILURE); }
else {
numerator = num;
denominator = denom; }

if (denominator < 0){
denominator = denominator * -1;
numerator = numerator * -1; }

simplify (numerator, denominator);
}

//main.cpp 的适用摘录:

void outputExpression(ostream & out,
const Fraction & num1,
const Fraction & num2,
const Fraction & result,
bool isSum);

int main() {
Fraction num1, num2, result;

num1 = Fraction(1, 2);
num2 = Fraction(2, 3);
result = num1.sumWith(num2);
outputExpression(cout, num1, num2, result, true);
result = num1.multiplyWith(num2);
outputExpression(cout, num1, num2, result, false);
cout << endl;

最佳答案

我仔细阅读了你的代码,并找出了你编写这段代码的场景,您的代码中存在一些严重的问题:1- 功能更改的定义必须采用以下格式:

int Fraction::change(int* a, int* b){
int temp;
temp = *a;
*a = *b;
*b = temp;
return *temp; }

2- 在您的构造函数中调用简化函数,

simplify (numerator, denominator);

同样在您的简化函数中,您调用分数构造函数:

simplify (numerator, denominator);

这是一个无限循环,会导致过度使用堆空间(超出一个程序使用堆的合法数量)。

3 - 在简化函数中,你写:

if ((r != 1) && (r != 0)){
for (int i = 0; r > 1; i++){
r = (n % d);
d = n;
r = d;
i++; }

这是一个问题,因为你必须在 d=n 之前将 d 保存在一个临时变量中;

否则下一个命令 (r = d;) 将等价于 r = n;

您必须解决此逻辑错误才能解决碎片错误。在这种情况下导致的错误:

当程序使用的堆空间大于其合法使用的堆空间时。在某些情况下,生成对象或获取内存 (alloc | malloc) 会陷入无限循环。

关于c++ - 在构造函数定义中调用函数后收到 Segmentation Fault 11 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29740265/

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