Access violation when trying to add an element in a DynamicArray [closed]

I can’t figure out why i get this error. (My guess would be that perhaps i have to add something to Movie class so that i can use the "=" sign for allocating it, but i don’t know how)

Well, i have a DynamicVector class which contains elements of type "Movie"

template <class T> class DynamicVector
{
    private:
        // arr is the integer pointer
        // which stores the address of our vector
        T* vector;

        // capacity is the total storage
        // capacity of the vector
        int capacity;

        // current is the number of elements
        // currently present in the vector
        int nr_elements;

    public:
        // Default constructor to initialise
        // an initial capacity of 1 element and
        // allocating storage using dynamic allocation
        DynamicVector(){

            vector = new T[2];
            capacity = 2;
            nr_elements = 0;
        }

        // destructor
        ~DynamicVector(){
            delete[] vector;
        }

        // increases the capacity of the vector if needed
        void match_capacity(){
            if (nr_elements == capacity) {
                T* temporary_vector = new T[2 * capacity];

                // copying old vector elements to the new one
                for (int i = 0; i < capacity; i++)
                    temporary_vector[i] = vector[i];

                // deleting previous vector
                delete[] vector;
                capacity *= 2;
                vector = temporary_vector;
            }
        }

        // Function to add an element at the last
        void add(T new_element){

            // If the current vector is full, we make it bigger
            match_capacity();

            // Inserting data
            vector[nr_elements] = new_element;
            nr_elements++;
        }

The Movie class looks like this:

//Movie.h
class Movie {

    private:
        string title;
        string genre;
        int year;
        int likes;
        string trailer;

    public:
    Movie(string title="", string genre="", int year=0, int likes=0, string trailer="");
    ~Movie();
    string get_title() const;
    string get_genre() const;
    int get_year() const;
    int get_likes() const;
    string get_trailer() const;
    void set_title(string new_title);
    void set_genre(string new_genre);
    void set_year(int new_year);
    void set_likes(int new_likes);
    void set_trailer(string new_trailer);
    string to_string() const;
};
Movie::Movie(string title, string genre, int year, int likes, string trailer){
    this->title = title;
    this->genre = genre;
    this->year = year;
    this->likes = likes;
    this->trailer = trailer;
}

Movie::~Movie() = default;

std::string Movie::get_title() const {
    return this->title;
}

std::string Movie::get_genre() const {
    return this->genre;
}

int Movie::get_year() const {
    return this->year;
}

int Movie::get_likes() const {
    return this->likes;
}

std::string Movie::get_trailer() const {
    return this->trailer;
}

void Movie::set_title(string new_title){
    this->title = new_title;
}

void Movie::set_genre(string new_genre){
    this->genre = new_genre;
}

void Movie::set_year(int new_year){
    this->year = new_year;
}

void Movie::set_likes(int new_likes){
    this->likes = new_likes;
}

void Movie::set_trailer(string new_trailer){
    this->trailer = new_trailer;
}

string Movie::to_string() const{
    string s;
    s += "Movie with title: " + this->title;
    s += "ntgenre: " + this->genre;
    s += "ntyear: " + std::to_string(this->year);
    s += "ntlikes: " + std::to_string(this->likes);
    s += "nttrailer: " + this->trailer;
    return s;
}

I have used the class DynamicVector in a movie database class, and when i try to add a new movie to it using the function below, the code exits with: "Exception 0xc0000005 encountered at address 0x70ff411f: Access violation writing location 0x00200020"

this->movies.add(m);
//this->movies is an instance of type DynamicVector
        void add(T new_element){

            // If the current vector is full, we make it bigger
            match_capacity();

            // Inserting data
            // -----------LINE BELOW CRASHES---------------
            vector[nr_elements] = new_element;
            nr_elements++;

        }

Source: Windows Questions C++

LEAVE A COMMENT