Frage

I am writting a program what is something like an information system for cinema. I am a begginner in C++.

After each compile I am getting this error alert message. I am sure that it is getting error when dectructor is calling.

I read a lot of posts which are relating to this error. But I still can not help. I have another classes but I do not think that error is there. Because when I comment content in destructor there is not any error after compilation. If you need I will post a code of another classes in my project.

Please help me if you can. Thank you for your willingness and assistance.

This is my code of class where it is getting error. Sorry for my terrible english

HEADER FILE

#pragma once
#include "Film.h"
#include <string>
using namespace std;

class Kino
{
private:
    Film* aFilmy;
    unsigned short int aPocetFilmov;
    string aNazov;


public:
    Kino(void);
    ~Kino(void);

    void nastavNazov(string paNazov);
    void pridajFilm(string paNazov);
    void vypisFilmy();

    Film* dajFilm(string paNazov);
    string dajNazov(){ return aNazov; };    
};

CPP FILE

#include "Kino.h"

Kino::Kino(void){
    aFilmy = NULL;
    aPocetFilmov = 0;
}

Kino::~Kino(void){
    if(aFilmy != NULL){
        delete[] aFilmy;
        aFilmy = NULL;
    }
}

void Kino::pridajFilm(string paNazov){
    Film film(paNazov);

    if(aFilmy == NULL){
        aFilmy = new Film[1];
        aFilmy[0] = film;
        aPocetFilmov++;
    }else{
        Film* temp = aFilmy;
        aFilmy = new Film[aPocetFilmov + 1];

        for(unsigned short int i = 0; i < aPocetFilmov; i++){
            aFilmy[i] = temp[i];
        }

        aFilmy[aPocetFilmov] = film;
        aPocetFilmov++;

        delete [] temp;     
    }
}

void Kino::vypisFilmy(){
    for(unsigned short int i = 0; i < aPocetFilmov; i++){
        cout << "[" << i << "] - " << aFilmy[i].dajNazov() << endl;
    }
}

Film *Kino::dajFilm(string paNazov){

    for(unsigned short int i = 0; i < aPocetFilmov; i++){
        if(aFilmy[i].dajNazov() == paNazov){
            return &aFilmy[i];
        }
    }

    return NULL;
}

Film.h - Movie

#pragma once
#include <string>
#include "Sala.h"
using namespace std;


class Film
{
private:
    string aNazov;
    Sala aSala;

public:
    Film(string paNazov);
    Film(void);
    ~Film(void);

    string dajNazov(){ return aNazov; };
    Sala* dajSalu(){ return &aSala; };
};

Film.cpp - Movie

#include "Film.h"
using namespace std;

Film::Film(string paNazov){
    aNazov = paNazov;

}

Film::Film(void){
    aNazov = "Neuvedeny";
}

Film::~Film(){

}

Sala.h - It is a hall in cinema

#pragma once
#include <string>
#include <iostream>
#include "Rad.h"
using namespace std;

class Sala
{
private:
    static const int aMaxPocetRadov = 10;
    Rad* aRady;
    string aNazov;

public:
    Sala(string paNazov);
    Sala(void);
    ~Sala(void);

    void vytvorSalu();
    void nastavNazov(string paNazov){ aNazov = paNazov; };
    void vypisRady();

    Rad *dajRad(int paCislo);
    int dajMaxPocetRadov(){ return aMaxPocetRadov; };
    string dajNazov(){ return aNazov; };
};

Sala.cpp - Hall.cpp

#include "Sala.h"

Sala::Sala(string paNazov){
    aNazov = paNazov;
    aRady = new Rad[aMaxPocetRadov];
    this->vytvorSalu();
}

Sala::Sala(void){
    aNazov = "Neuvedene";
    aRady = new Rad[aMaxPocetRadov];
    this->vytvorSalu();
}

Sala::~Sala(){
    delete [] aRady;
    aRady = 0;

}

void Sala::vytvorSalu(){
    for(unsigned short int i = 0; i < aMaxPocetRadov; i++)
        aRady[i].nastavCisloRadu(i + 1);
}

void Sala::vypisRady(){
    Rad* rad;
    for(unsigned short int i = 0; i < aMaxPocetRadov; i++){
        rad = this->dajRad(i+1);
        if(rad != NULL){
            cout << rad->toString() << endl;
        }
    }
}


Rad *Sala::dajRad(int paCisloRadu){
    if(paCisloRadu > 0 && paCisloRadu <= aMaxPocetRadov){
        return &aRady[paCisloRadu-1];
    }

    return NULL;
}

Even so it is not everything. If that will not help I post other 3 last classes

Best regards Radovan Greetings from Slovakia

War es hilfreich?

Lösung

One major problem with your code is the lack of a proper copy-ctor. To test this, add

Kino( const Kino& ) = delete;

to your class. This will show you where a copy is made. Why is this a problem? Because the automatically generated default copy-ctor (in case you don't add something like the above or a real copy-ctor) will copy the pointer to the allocated films. But you now have two instances. When the first instance of Kino is deleted, it calls delete[] aFilmy; and the second instance now has a pointer pointing to the already deleted memory. When the second instance is deleted, it tries to free the memory block again - and that's what is causing the problem for you.

You should either delete the copy-ctor (and the assigment-operator) with

Kino( const Kino& ) = delete;
void operator=( const Kino& ) = delete;

or define them and make sure you create a proper copy of the data.

See the Rule of Five (formerly Rule of Three).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top