分段错误(核心转储)无限递归

时间:2014-11-29 19:46:27

标签: c++

当我运行程序时,我遇到了分段错误。有人告诉我,有些代码会导致无限循环并导致程序崩溃,我认为这是正在发生的事情。但是他们并没有告诉我代码的部分内容,所以这只是猜测。另外,有人可以告诉我,当我重载"<<"和">>"运营商?在用g ++ -Wall编译之后,它警告我函数中没有返回语句,它不是void类型。

我将提供我正在使用的三个文件的源代码:

main.cc

#include <iostream>
#include "my_int.h"
using namespace std;

int main()
{
    my_int value1;
    my_int value2(5);

    // testing the constructors

    cout <<" Value 1 contains "<<value1 <<endl <<endl;
    cout <<" Value 2 contains "<<value2 <<endl <<endl;

    // testing the extraction and insertion operators
    cout <<"Enter an int value -- >  ";
    cin >> value1;
    cout <<"Value1 now contains "<<value1<<endl<<endl;

    // testing binary operators

    cout << value1 << " + " << value2 << " = " << value1 + value2 <<endl <<endl;
    cout << value1 << " - " << value2 << " = " << value1 - value2 <<endl <<endl;
    cout << value1 << " * " << value2  << " = " << value1 * value2  <<endl <<endl;
    cout << value1 << " / " << value2 << " = " << value1 / value2 <<endl <<endl;

    cout << value1 << " * " << 3  << " = " << value1 * 3  <<endl <<endl;

    // testing the % operator

    if (value1 % 2 == 0)
        cout << value1 << " is even  \n\n";
    else
        cout << value1 << " is odd \n\n";

    // testing comparison operators

    if (value1 <= value2)
        cout<<value1 << " <= "<< value2 <<endl;
    else
        cout<<value1 << " > "<< value2 <<endl;

    if (value1 == value2)
       cout<<value1 << " == "<< value2 <<endl;
    else
       cout<<value1 << " != "<< value2 <<endl;

    // testing increment and decrement operators

    ++value1;
    cout <<" increment the value1 by 1 "<< value1 << endl;

    --value2;
    cout <<" decrement the value2 by 1 "<< value2 << endl;

    // testing the set function

    value1.set(25);

    cout <<" Value1 now contains  "<< value1 <<endl;

    return 0;
}

my_int.cc

#include <iostream>
#include "my_int.h"
using namespace std;

my_int::my_int(int x){
    val = x;
}

my_int::my_int(){
    val = 0;
}

void my_int::set(int num){
    val = num;
}

my_int operator +(const my_int& num1, const my_int& num2){
    my_int temp;
    temp = num1 + num2;
    return(temp);
}

my_int operator -(const my_int& num1, const my_int& num2){
    my_int temp;
    temp = num1 - num2;
    return(temp);
}

my_int operator %(const my_int& num1, int num2){
    my_int temp;
    temp = num1%num2;
    return(temp);
}

my_int operator /(const my_int& num1, const my_int& num2){
    my_int temp;
    temp = num1/num2;
    return(temp);
}

my_int operator *(const my_int& num1, int num2){
    my_int temp;
    temp = num1*num2;
    return(temp);
}

my_int operator *(const my_int& num1, const my_int& num2);

void my_int::operator ++(){
    val += 1;
}

void my_int::operator --(){
    val -= 1;
}

bool operator ==(const my_int& num1, const my_int& num2){
    return(num1 == num2);
}

bool operator <=(const my_int& num1, const my_int& num2){
    return(num1 <= num2);
}

istream& operator >>(istream& inp, my_int& num){
    inp >> num;
}

ostream& operator <<(ostream& outp, const my_int& num){
    outp << num;
}

int my_int::get_val()const{
    return(val);
}

my_int.h

#include <iostream>
using namespace std;

class my_int
{
public:
    //constructors
    my_int(int x);
    my_int();

    // friend functions
    friend my_int operator + (const my_int& num1, const my_int& num2);

    friend my_int operator - (const my_int& num1, const my_int& num2);

    friend my_int operator % (const my_int& num1,  int num2);

    friend my_int operator *(const my_int& num1, const my_int& num2);

    friend bool operator <= (const my_int& num1, const my_int& num2);

    friend istream& operator >> (istream& inp,  my_int& num);

    friend ostream& operator << (ostream& outp, const my_int& num);

    //mutators
    void set(int num);
    void operator ++();
    void operator --();

    // accessors
    int get_val()const;

 private:
     int val;
};

// non-member operators

my_int operator * (const my_int& num1, int num2);

my_int operator / (const my_int& num1, const my_int& num2);

bool operator == (const my_int& num1, const my_int& num2);

非常感谢你们提供的任何帮助!

2 个答案:

答案 0 :(得分:2)

所有操作符都会导致无限循环:

my_int operator +(const my_int& num1, const my_int& num2){

    my_int temp;
    temp = num1 + num2; // this uses operator+(const my_int& num1, const my_int& num2)

    return(temp);

}

必须:

my_int operator +(const my_int& num1, const my_int& num2){

    return my_int( num1.val + num2.val); // this uses pre-defined operator+(int num1, int num2)

}

-%*==,...的相同评论

对于您的下一个开发,我建议您编写一个函数,测试它,编写另一个函数,测试它等等。然后你可以逐个修复错误; - )

答案 1 :(得分:0)

在此功能中,您忘记返回值:

istream& operator >>(istream& inp, my_int& num){
    inp >> num;
    return inp; //You forgot this line
}

ostream& operator <<(ostream& outp, const my_int& num){
    outp << num;
    return outp; //and this one
}

接下来,缺少以下函数的实现:

my_int operator *(const my_int& num1, const my_int& num2);

您的运营商+-*/%正在造成无限循环。您需要增加一个值,而不是再次调用您的运算符...