Ошибка памяти на C++

Я написал класс VectorN, который сравнивает обьект, но у меня проблема с ошибкой выделения памяти, помогитевведите сюда описание изображения


//VectorN.h
#pragma once
#include <string>

class VectorN {
private:
    int N;
    double* a;
public:
    VectorN() {
        N = 0;
        a = nullptr;    
    };

    VectorN(int N, double* a) {
        this->N = N;
        for (int i = 0; i < N; ++i) {
            this->a[i] = a[i];
        }
    }
    VectorN(const VectorN& other) {
        N = other.N;
        for (int i = 0; i < N; ++i) {
            a[i] = other.a[i];
        }
    }

    ~VectorN() {
    }

    void setValues();
    void print() const;
    
    friend VectorN multscal(const VectorN& a, double value);
    double length() const;
    bool isShorterThan(const VectorN& vector1) const;
    bool isLongerThan(const VectorN& vector1) const;
    bool isSimilar(const VectorN& vector1) const;
    bool isEqual(const VectorN& other) const;
    operator std::string() const;
    bool Init(int N);
    void Read();
    void Display() const;

    void clear();
    double* getArray() const { return a; }
    void setArray(double* newArray);
    int getN() const { return N; };
    void setN(int value) { N = value; };
    std::string toString() const;

    friend std::ostream& operator<<(std::ostream& out, const VectorN& integer);

    friend std::istream& operator>>(std::istream& in, VectorN& integer);\
    VectorN& operator=(const VectorN& other);
    
    VectorN& operator ++();
    VectorN& operator --();
    VectorN operator ++(int);
    VectorN operator --(int);
};

//VectorN.cpp
#include "VectorN.h"
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

void VectorN::setValues() {
    double x;
    for (int i = 0; i < N; i++) {
        cout << "Enter value: ";
        cin >> x;
        a[i] = x;
    }
}

void VectorN::print() const {
    cout << "(";
    for (int i = 0; i < N - 1; i++) {
        cout << a[i] << ", ";
    }
    cout << a[N - 1] << ")" << endl;
}

void VectorN::setArray(double* newArray) {

    if (a != nullptr) {
        delete[] a;
    }

    a = new double[N];
    for (int i = 0; i < N; i++) {
        a[i] = newArray[i];
    }
}

VectorN multscal(const VectorN& a, double value) {
    VectorN b;
    b.setN(a.getN());
    double* a_array = a.getArray();
    double* b_array = new double[b.getN()];


    for (int i = 0; i < b.getN(); i++) {
        b_array[i] = a_array[i] * value;
    }

    b.setArray(b_array);

    delete[] b_array;

    return b;
}

double VectorN::length() const {
    double sum = 0;
    for (int i = 0; i < N; i++) {
        sum += a[i] * a[i];
    }
    return sqrt(sum);
}

bool VectorN::isShorterThan(const VectorN& vector1) const {
    return length() < vector1.length();
}

bool VectorN::isLongerThan(const VectorN& vector1) const {
    return length() > vector1.length();
}

bool VectorN::isSimilar(const VectorN& vector1) const {
    return length() == vector1.length();
}

bool VectorN::isEqual(const VectorN& other) const {
    if (N != other.N) return false;
    for (int i = 0; i < N; i++) {
        if (a[i] != other.a[i]) return false;
    }
    return true;
}

bool VectorN::Init(int N) {
    return N > 0;
}


void VectorN::Display() const {
    getN();
    print();
    length();
}

std::string VectorN::toString() const {
    cout << "(";
    for (int i = 0; i < N; i++) {
        cout << "'" << a[i] << "'";
        if (i < N - 1) cout << ", ";
    }
    cout << ")" << endl;
    return "";
}

void VectorN::Read() {
    int value;
    VectorN a;
    cout << "Enter vector length: "; cin >> value;
    if (Init(value)) {
        a.setN(value);
    }
    else { cout << "Unaccaptable value";  exit(0); }
    a.a = new double[N];
    a.setValues();
    a.Display();
    int value1;
    cout << "Enter a const for scalar: "; cin >> value1;
    VectorN b;
    b.a = new double[a.N];
    b = multscal(a, value1);
    b.Display();
    a.clear();
    b.clear();
}

void VectorN::clear() {

    delete[] a;
}

VectorN& VectorN::operator=(const VectorN& other) {
    if (this != &other) {
        delete[] a;
        N = other.N;
        a = new double[N];
        for (int i = 0; i < N; ++i) {
            a[i] = other.a[i];
        }
    }
    return *this;
}

VectorN& VectorN::operator++() {
    for (int i = 0; i < N; ++i) {
        ++a[i];
    }
    return *this;
}

VectorN& VectorN::operator--() {
    for (int i = 0; i < N; ++i) {
        --a[i];
    }
    return *this;
}

VectorN VectorN::operator++(int) {
    VectorN t(*this);
    for (int i = 0; i < N; ++i) {
        a[i]++;
    }
    return t;
}

VectorN VectorN::operator--(int) {
    VectorN t(*this);
    for (int i = 0; i < N; ++i) {
        a[i]--;
    }
    return t;
}

std::ostream& operator<<(std::ostream& out, const VectorN& vector) {
    out << vector.N << " "; 
    for (int i = 0; i < vector.N; ++i) {
        out << vector.a[i] << " "; 
    }
    return out;
}


std::istream& operator>>(std::istream & in, VectorN & vector) {
    in >> vector.N;
    if (vector.Init(vector.N)) { // Check for allocation success
        vector.a = new double[vector.N];
        for (int i = 0; i < vector.N; ++i) {
            in >> vector.a[i];
        } 
    }
    return in;
}


VectorN::operator std::string() const {
    std::stringstream ss;
    ss << "VectorN size: " << N << std::endl;
    ss << "Elements: ";
    for (int i = 0; i < N; ++i) {
        ss << a[i] << " ";
    }
    return ss.str();
}

//Source.cpp

#include <iostream>
#include "VectorN.h"

using namespace std;

#pragma pack(1)

VectorN CreateVector() {
    VectorN vector;
    vector.Read();
    return vector;
}

int main() {
    VectorN vector = CreateVector(); 
    VectorN vector1 = CreateVector();

    if (vector.isEqual(vector1)) {
        cout << "Vectors are equal!" << endl;
    }

    if (vector.isShorterThan(vector1)) {
        cout << "Vector is shorter than Vector1!" << endl;
    }else if (vector.isSimilar(vector1)) {
        cout << "Vectors are similar in length!" << endl;
    }
    else {
        cout << "Vector1 is longer than Vector!" << endl;
    }

    vector.clear();
    vector1.clear();


    cout << "Size with pragma: " << sizeof(class VectorN) << endl;

    VectorN vec;

    cout << "Enter values for N & vector: "; cin >> vec;

    cout << "Postfix increment: " << vec++ << endl;
    cout << "Updated vec: " << vec << endl;
    cout << "Prefix increment: " << ++vec << endl;
    cout << "Updated vec: " << vec << endl;

    //string(vec);

    return 0;
}

Ответы (0 шт):