堆损坏和其他问题

问题描述 投票:0回答:1

我刚刚开始学习OOP,并且我的第一个项目遇到了一些问题。它不会每次都运行,但是在运行时会显示正确的值。当它不运行时,问题似乎出在问题上,艰难的是我重载了<


#include<iostream>
#include<cmath>

using namespace std;

class Numar_Complex {
    double re, im;
public:
    Numar_Complex() { this->im = 0; this->re = 0; }
    Numar_Complex(Numar_Complex const &n){
            this->re = n.re;
            this->im = n.im;
        }
    ~Numar_Complex() = default;
    double Modul();
    Numar_Complex operator + (Numar_Complex const& obj) {
        Numar_Complex res;
        res.re = this->re + obj.re;
        res.im = this->im + obj.im;
        return res;
    }
    Numar_Complex operator *(Numar_Complex const& obj) {
        Numar_Complex res;
        res.re = this->re * obj.re - this->im * obj.im;
        res.im = this->re * obj.im + this->im * obj.re;
        return res;
    }
    friend ostream& operator <<(ostream& out, Numar_Complex & obj) {
        out << obj.re << " + i * " << obj.im;
        return out;
    }
    friend istream& operator >>(istream& in, Numar_Complex &obj) {
        in >> obj.re >> obj.im;
        return in;
    }
    void operator =(const Numar_Complex& x) {
        this->re = x.re;
        this->im = x.im;
    }
};

class Vector_Complex : public Numar_Complex {
    int len;
    Numar_Complex* v;
public:
    Vector_Complex();
    Vector_Complex(Numar_Complex const &x, int n);
    Vector_Complex(Vector_Complex &w);
    ~Vector_Complex() = default;
    double* Get_VectorOfModules();
    int Get_Len() {
        return len;
    }
    void Sort();
    Numar_Complex Sum_Vector();
    Numar_Complex Prod_Scalar(Vector_Complex const&a, Vector_Complex const&b);
    friend ostream& operator <<(ostream& out, Vector_Complex const& w) {
        out << w.len << "\n";
        for (int i = 0; i < w.len; i++)
            out << w.v[i] << " ";
        return out;
    }
    friend istream& operator >>(istream& in, Vector_Complex & w) {
        in >> w.len;
        for (int i = 0; i < w.len; i++)
            in >> w.v[i];
        return in;
    }
};

int main()
{
    Vector_Complex Vec;
    cin >> Vec;
    cout << Vec << "\n" << "\n";
    double* p = Vec.Get_VectorOfModules();
    int n = Vec.Get_Len();
    for (int i = 0; i < n; i++, p++)
        cout << *p << " ";
    p -= n;
    delete[] p;
    return 0;
}


double Numar_Complex::Modul() {
    return sqrt(this->re * this->re + this->im * this->im);
}

Vector_Complex::Vector_Complex() {
    len = 0;
    v = new Numar_Complex[0];
}

Vector_Complex::Vector_Complex(Numar_Complex const &x, int n) {
    len = n;
    v = new Numar_Complex[n];
    for (int i = 0; i < n; i++)
        v[i] = x;
}

Vector_Complex::Vector_Complex(Vector_Complex &w) {
    len = w.len;
    v = new Numar_Complex[len];
    for (int i = 0; i < len; i++)
        v[i] = w.v[i];
}

double* Vector_Complex::Get_VectorOfModules() {
    double* w = new double[len];
    for (int i = 0; i < len; i++)
        w[i] = v[i].Modul();
    return w;
}

void Vector_Complex::Sort() {

}

Numar_Complex Vector_Complex::Sum_Vector() {
    Numar_Complex s;
    for (int i = 0; i < len; i++)
        s = s + v[i];
    return s;
}

Numar_Complex Vector_Complex::Prod_Scalar(Vector_Complex const& a, Vector_Complex const& b) {
    Numar_Complex prod;
    for (int i = 0; i < a.len; i++)
        prod = prod + a.v[i] * b.v[i];
    return prod;
}

尝试输入31 22 53 5希望您能帮助我,我尝试将代码修复5小时,但没有成功。

c++ oop heap-memory
1个回答
1
投票

您默认构造一个Vector_Complex对象,而您的默认构造函数则是

Vector_Complex::Vector_Complex() {
    len = 0;
    v = new Numar_Complex[0];
}

然后您从流中读取到Vector_Complex对象中,确实如此

friend istream& operator >>(istream& in, Vector_Complex & w) {
    in >> w.len;
    for (int i = 0; i < w.len; i++)
        in >> w.v[i];
    return in;
}

现在,由于Vector_Complex对象是默认构造的,因此v变量将是指向zero个元素的“数组”的指针。这意味着w.v[i]对于iany值无效。用in >> w.v[i]进行写入会导致未定义的行为

© www.soinside.com 2019 - 2024. All rights reserved.