C++ Initialize array in constructor EXC_BAD_ACCESS

Posted by user890395 on Stack Overflow See other posts from Stack Overflow or by user890395
Published on 2011-11-21T17:43:04Z Indexed on 2011/11/21 17:50 UTC
Read the original article Hit count: 230

I'm creating a simple constructor and initializing an array:

// Construtor
Cinema::Cinema(){
    // Initalize reservations
    for(int i = 0; i < 18; i++){
        for(int j = 0; j < 12; j++){
            setReservation(i, j, 0);
        }
    }

    // Set default name
    setMovieName("N/A");

    // Set default price
    setPrice(8);
}

The setReservation function:

void Cinema::setReservation(int row, int column, int reservation){
    this->reservations[row][column] = reservation;
}

The setMovieName function:

void Cinema::setMovieName(std::string movieName){
    this->movieName = movieName;
}

For some odd reason when I run the program, the setMovieName function gives the following error: "Program Received Signal: EXC_BAD_ACCESS"

If I take out the for-loop that initializes the array of reservations, the problem goes away and the movie name is set without any problems. Any idea what I'm doing wrong?

This is the Cinema.h file:

#ifndef Cinema_h
#define Cinema_h

class Cinema{

private:
    int reservations[17][11];
    std::string movieName;
    float price;
public:
    // Construtor
    Cinema();

    // getters/setters
    int getReservation(int row, int column);
    int getNumReservations();
    std::string getMovieName();
    float getPrice();

    void setReservation(int row, int column, int reservation);
    void setMovieName(std::string movieName);
    void setPrice(float price);
};

#endif

© Stack Overflow or respective owner

Related posts about c++

Related posts about arrays