当我明确调用类的析构函数时出现错误

时间:2019-04-03 16:08:12

标签: c++

所以我有一个Vector类,例如当我在main中调用对象的析构函数时,我得到一个异常。异常文件中的代码行是“ free_dbg(block,_UNKNOWN_BLOCK)”;请注意,如果我仅读取向量或仅执行析构函数调用,则不会得到异常。当我一起做时,这就是问题出现的时间。 这是代码:

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

class Vector
{
public:
    Vector();
    ~Vector();
    Vector(const Vector& vec);
    void insert_element(int value, int position);
    void delete_element(int position);
    void set_nr_elem(int nr) { nr_elem = nr; }
    int get_nr_elem() const { return nr_elem; }
    int get_element(int position) const { return arr[position]; }
    friend istream& operator>>(istream& input, Vector &vec);
    friend ostream& operator<<(ostream& output, const Vector &vec);
    Vector operator=(const Vector& vec);
    void free_memory() { delete[] arr; }
private:
    int *arr;
    int nr_elem;
};

Vector::Vector()
{
    arr = NULL;
    nr_elem = 0;
}

Vector::~Vector()
{
    delete[]arr;
    nr_elem = 0;
}

Vector::Vector(const Vector& vec)
{
    arr = new int[vec.nr_elem];
    nr_elem = vec.nr_elem;
    for (int i = 0; i < nr_elem; i++)
        arr[i] = vec.arr[i];
}

void Vector::insert_element(int value, int position)
{
    if (position < 0 || position > nr_elem)
    {
        cout << "Invalid position";
        return;
    }
    arr = (int*)realloc(arr, (nr_elem + 1) * sizeof(int));
    for (int i = nr_elem; i >= position + 1; i--)
        arr[i] = arr[i - 1];
    arr[position] = value;
    nr_elem++;
}

void Vector::delete_element(int position)
{
    if (position < 0 || position >= nr_elem)
    {
        cout << "Invalid position";
        return;
    }
    for (int i = position; i < nr_elem; i++)
        arr[i] = arr[i + 1];
    delete &arr[nr_elem];
    nr_elem--;
}

istream& operator>>(istream& input, Vector &vec)
{
    int nr;
    input >> nr;
    for (int i = 0; i < nr; i++)
    {
        int x;
        input >> x;
        vec.insert_element(x, vec.get_nr_elem());
    }
    return input;
}

ostream& operator<<(ostream& output, const Vector &vec)
{
    if (vec.get_nr_elem())
    {
        output << "Number of elements: " << vec.get_nr_elem() << "\n";
        output << "The elements: ";
        for (int i = 0; i < vec.get_nr_elem(); i++)
            output << vec.get_element(i) << " ";
        output << "\n";
    }
    else
    {
        output << "The vector has no elements";
    }
    return output;
}

Vector Vector::operator=(const Vector& vec)
{
    if (this == &vec)
        return *this;
    delete[] arr;
    arr = new int[vec.nr_elem];
    for (int i = 0; i < vec.nr_elem; i++)
        arr[i] = vec.arr[i];
    nr_elem = vec.nr_elem;
    return *this;
}

class Natural_Big_Number 
{
public:
    void set_sign(char c) { sign = c; }
    char get_sign() const { return sign; }
    friend istream& operator>>(istream& input, Natural_Big_Number &number);
    friend ostream& operator<<(ostream& output, const Natural_Big_Number &number);
    friend Natural_Big_Number operator+(const Natural_Big_Number& number1, const Natural_Big_Number& number2);
    friend Natural_Big_Number operator-(const Natural_Big_Number& number1, const Natural_Big_Number& number2);
    friend Natural_Big_Number& operator*(const Natural_Big_Number& number1, const Natural_Big_Number& number2);
    friend Natural_Big_Number& operator/(const Natural_Big_Number& number1, const Natural_Big_Number& number2);
    Natural_Big_Number operator=(const Natural_Big_Number& number);

private:
    Vector vec;
    char sign;
};

istream& operator>>(istream& input, Natural_Big_Number &number)
{
    int x;
    input >> x;
    char c;
    input.get();
    c = input.get();
    if (c == '-')
        number.set_sign('-');
    else
    {
        number.set_sign('+');
        int digit = (int)c - 48;
        number.vec.insert_element(digit, number.vec.get_nr_elem());
    }
    while ((c = input.get()) >= '0' && c <= '9')
    {
        int digit = (int)c - 48;
        number.vec.insert_element(digit, number.vec.get_nr_elem());
    }
    return input;
}

ostream& operator<<(ostream& output, const Natural_Big_Number &number)
{
    cout << "Number of digits: " << number.vec.get_nr_elem() << '\n';
    cout << "The number: ";
    if (number.get_sign() == '-')
        cout << number.get_sign();
    for (int i = 0; i < number.vec.get_nr_elem(); i++)
        cout << number.vec.get_element(i);
    cout << endl;
    return output;
}

Natural_Big_Number operator+(const Natural_Big_Number& number1, const Natural_Big_Number& number2)
{
    Natural_Big_Number result;
    int carry = 0;
    int it_digits1 = number1.vec.get_nr_elem() - 1;
    int it_digits2 = number2.vec.get_nr_elem() - 1;
    if (number1.get_sign() == number2.get_sign())
    {
        result.set_sign(number1.get_sign());
        while (it_digits1 >= 0 && it_digits2 >= 0)
        {
            int aux = number1.vec.get_element(it_digits1) + number2.vec.get_element(it_digits2) + carry;
            int value = aux % 10;
            result.vec.insert_element(value, 0);
            carry = aux / 10;
            it_digits1--;
            it_digits2--;
        }
        for (int i = 0; i <= it_digits1; i++)
        {
            int aux = number1.vec.get_element(it_digits1) + carry;
            int value = aux % 10;
            carry = aux / 10;
            result.vec.insert_element(value, 0);
        }
        for (int i = 0; i <= it_digits2; i++)
        {
            int aux = number2.vec.get_element(it_digits2) + carry;
            int value = aux % 10;
            carry = aux / 10;
            result.vec.insert_element(value, 0);
        }
    }
    /*else if (number1.get_sign() == '-' && number2.get_sign() == '+')
    {
        result = number1;
        result.set_sign('+');
        result = number2 + result;
    }*/
    cout << result;
    return result;
}

Natural_Big_Number operator-(const Natural_Big_Number& number1, const Natural_Big_Number& number2)
{
    Natural_Big_Number result;
    int carry = 0;
    int it_digits1 = number1.vec.get_nr_elem() - 1;
    int it_digits2 = number2.vec.get_nr_elem() - 1;
    while (it_digits1 >= 0 && it_digits2 >= 0)
    {
        int aux = number1.vec.get_element(it_digits1) - number2.vec.get_element(it_digits2) + carry;
        int value = aux % 10;
        result.vec.insert_element(value, 0);
        carry = aux / 10;
        it_digits1--;
        it_digits2--;
    }
    for (int i = 0; i <= it_digits1; i++)
    {
        int aux = number1.vec.get_element(it_digits1) + carry;
        int value = aux % 10;
        carry = aux / 10;
        result.vec.insert_element(value, 0);
    }
    for (int i = 0; i <= it_digits2; i++)
    {
        int aux = number2.vec.get_element(it_digits2) + carry;
        int value = aux % 10;
        carry = aux / 10;
        result.vec.insert_element(value, 0);
    }
    cout << result;
    return result;
}

Natural_Big_Number Natural_Big_Number::operator=(const Natural_Big_Number& number)
{
    if (this == &number)
        return *this;
    vec.free_memory();
    set_sign(number.get_sign());
    vec = number.vec;
    return *this;
}

int main()
{
    ifstream f("date.in");
    Natural_Big_Number number1, number2;
    f >> number1 >> number2;
    number1 = number2;
    cout << number1;

    return 0;
}

1 个答案:

答案 0 :(得分:4)

那是因为您不应该这样做

Private Sub Printout1_Click() Dim Size As Integer Size = Me.ListBox2.ListCount - 1 ReDim ListBoxContents(0 To Size) As String Dim i As Integer For i = 0 To Size ListBoxContents(i) = Me.ListBox2.List(i) Next i For i = 0 To Size Sheets(ListBoxContents(i)).Visible = True Sheets(ListBoxContents(i)).Select ActiveWindow.SelectedSheets.PrintPreview Sheets(ListBoxContents(i)).Visible = False Next i End Sub 超出范围时会调用析构函数。

您的程序尝试破坏vec1两次,这会导致混乱