2014-12-05 48 views
0

目前我的程序在顯示250行之後顯示一行後崩潰。這裏是我的代碼:從打印功能崩潰的C++程序

string MovieList::PrintAll() { 
    for (int i = 0; i < last_movie_index; i++) { 
     movies[last_movie_index].Movie::PrintMovie(); 
    } 
} 

string Movie::PrintMovie() { 
    cout << fixed << setprecision(1) << rating << "\t\t" << votes << "\t\t" << "(" << 
      year_made << ")" << "\t" << name << endl; 
} 

全部電影和MovieList類:

class Movie { 
public: 
    Movie(); 
    Movie(string n, int y, int v, double r); 
    string get_name(); 
    void set_name(string n); 
    int get_year(); 
    void set_year(int y); 
    int get_votes(); 
    void set_votes(int v); 
    double get_rating(); 
    void set_rating(double r); 
    string PrintMovie(); 

private: 
    string name; 
    int year_made; 
    int votes; 
    double rating; 

}; 

Movie::Movie() { 
    name = "null"; 
    year_made = 0; 
    votes = 0; 
    rating = 0.0; 
} 

Movie::Movie(string n, int y, int v, double r) { 
    name = n; 
    year_made = y; 
    votes = v; 
    rating = r; 
} 

string Movie::get_name() { 
    return name; 
} 

void Movie::set_name(string n) { 
    name = n; 
} 

int Movie::get_year() { 
    return year_made; 
} 

void Movie::set_year(int y) { 
    year_made = y; 
} 

int Movie::get_votes() { 
    return votes; 
} 

void Movie::set_votes(int v) { 
    votes = v; 
} 

double Movie::get_rating() { 
    return rating; 
} 

void Movie::set_rating(double r) { 
    rating = r; 
} 

string Movie::PrintMovie() { 
    cout << fixed << setprecision(1) << rating << "\t\t" << votes << "\t\t" << "(" << 
      year_made << ")" << "\t" << name << endl; 
} 

class MovieList { 
public: 
    MovieList(int size); 
    ~MovieList(); 
    int Length(); 
    bool IsFull(); 
    void Add(Movie const& m); 
    string PrintAll(); 

private: 
    Movie* movies; 
    int last_movie_index; 
    int movies_size; 
    int movie_count = 0; 

}; 

MovieList::MovieList(int size) { 
    movies_size = size; 
    movies = new Movie[movies_size]; 
    last_movie_index = -1; 
} 

MovieList::~MovieList() { 
    delete [] movies; 
} 

int MovieList::Length() { 
    return last_movie_index; 
} 

bool MovieList::IsFull() { 
    return last_movie_index == movies_size; 
} 

void MovieList::Add(Movie const& m) 
{ 
    if (IsFull()) { 
     cout << "Cannot add movie, list is full" << endl; 
     return; 
    } 
    last_movie_index++; 
    movies[last_movie_index] = m; 
} 

string MovieList::PrintAll() { 
    for (int i = 0; i < last_movie_index; i++) { 
     movies[i].Movie::PrintMovie(); 
    } 
} 

我的陣列電影是動態分配的(即movies = new Movie[movies_size];)。我注意到使用cout << movies[1] << endl在PrintAll函數中不起作用。這是爲什麼它可能崩潰?我能做些什麼來解決它?

+2

should't它是_moves [I] _而不是_moves [last_movie_index] _在循環中? – alexm 2014-12-05 23:30:35

+0

@alexm Ahhh是的,我覺得這是一個白癡。不幸的是,它在第​​一次印刷之後仍然崩潰。現在會發生什麼呢會打印: 9.2 453500(1994)The Shawshank Redemption 0.0 13483293892(-5963777736) 然後它會崩潰在我身上 – andayn 2014-12-05 23:35:28

+0

請確保您正在初始化Movie類的成員。看起來'votes'和'year_made'沒有從Shawshank Redemption輸出行中初始化。 如果您發佈該類的定義,它會更容易。 此外,在調用PrintMovie()函數時,沒有必要包含'Movie ::'資格。 – 2014-12-05 23:38:54

回答

0

這不會解決你的問題,但如果你想cout << movies[i] << endl工作,那麼你需要在電影類定義以下功能:

friend ostream& operator<<(ostream& ostr, const Movie& movie){ 
    ostr << fixed << setprecision(1) << rating << "\t\t" << votes << "\t\t" << "(" << 
      year_made << ")" << "\t" << name ; 
} 
+1

我不認爲函數聲明是被允許嵌套的。這是一個錯字嗎? – 2014-12-06 00:13:45

+0

謝謝,那是一個複製粘貼錯誤 – 2014-12-10 03:57:01