Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++程序从打印功能中崩溃_C++ - Fatal编程技术网

C++程序从打印功能中崩溃

C++程序从打印功能中崩溃,c++,C++,目前,我的程序在显示了它应该显示的250行中的一行之后崩溃了。这是我的密码: string MovieList::PrintAll() { for (int i = 0; i < last_movie_index; i++) { movies[last_movie_index].Movie::PrintMovie(); } } string Movie::PrintMovie() { cout << fixed << setpr

目前,我的程序在显示了它应该显示的250行中的一行之后崩溃了。这是我的密码:

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;
}
完整的电影和电影艺术家课程:

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();
   }
}

我的阵列电影是动态分配的,即电影=新电影[电影大小];。我注意到使用cout并不能解决你的问题,但如果你想做cout,那么循环中的动作[最后一部电影索引]不应该是moves[I]吗?@alexm Ahhh是的,我觉得这是个白痴。不幸的是,在第一次打印之后,它仍在崩溃。现在将要发生的是,它将打印:9.2 453500 1994《肖申克的救赎》0.0 13483293892-5963777736,然后它将在我身上崩溃。请确保您正在初始化电影类的成员。看起来选票和所做的年份并不是从肖申克的救赎输出行初始化的。如果您发布了该类的定义,那么会更容易。另外,在调用PrintMovie函数时,没有理由包含Movie::qualification。@Wescamberland我编辑了OT并发布了完整的Movie和MovieList类(如果有帮助的话)。投票结果和年份将在Shawshank Redemption(肖申克的救赎)上进行初始化,紧接着一切都会在崩溃之前失控。在另一个问题中,您已经被告知,如果您的函数返回一个值,您应该有一个返回语句。罕见的标准兼容异常:int main不必有return语句,默认为返回零。为什么?”因为——这就是原因。因此,有返回PrintAll和printmoine函数的字符串,但没有返回语句。请为并非所有返回值的路径启用并读取警告,我认为不允许嵌套函数声明。这是打字错误吗?
friend ostream& operator<<(ostream& ostr, const Movie& movie){
    ostr << fixed << setprecision(1) << rating << "\t\t" << votes << "\t\t" << "(" <<
            year_made << ")" << "\t" << name ;
}