gpt4 book ai didi

C++函数导致应用程序崩溃并且无法正常工作

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:20:18 25 4
gpt4 key购买 nike

我的应用程序出现问题,我的 PrintAll 函数无法正常工作,最终只会使我的应用程序崩溃。我的应用程序应该从文件中读取字符串并将它们插入到数组中。问题是它读取不正确,最终会使我的应用程序崩溃。这是我认为问题所在:

int main()
{
LoadMovies();

MovieList *movies = LoadMovies();
//movies->MovieList::PrintAll();

// // test methods for the Movie and MovieList classes
//PrintAllMoviesMadeInYear(movies, 1984);
//PrintAllMoviesWithStartLetter(movies, 'B');
//PrintAllTopNMovies(movies, 5);

//delete movies;
return 0;
}

MovieList* LoadMovies()
{
vector<string> movies;
ReadMovieFile(movies);
MovieList ml = MovieList(movies.size());

string name;
int year;
double rating;
int votes;

for (int i = 0; i < movies.size(); i++)
{
istringstream input_string(movies[i]);
getline(input_string, name, '\t');
input_string >> year >> rating >> votes;
Movie movie (name, year, votes, rating);
ml.Add(movie);
}
ml.PrintAll();
}

完整示例:

/*
* File: MovieStatsProgram.cpp
* Author:
* Date:
* ===============================================================
* This is a console app to test the Movie and MovieList classes.
*
* TODO:
*
* You need to finish the implementation of the loadMovies method
* to create and initialize the MovieList object.
*
* You also need to create three static methods:
*
* PrintAllMoviesMadeInYear - it will print all the movies made in a
* given year once sort in alphabetical order and once sorted by the number
* of votes with the movie with the most number of votes printed first.
*
* PrintAllMoviesWithStartLetter - it will print all the movies started with
* a given letter sorted in alphabetical order
*
* PrintAllTopNMovies - it will display the top N movies based on the number of
* votes
*/

#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

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[last_movie_index].Movie::PrintMovie();
//cout << movies[last_movie_index] << endl;
}
}

void ReadMovieFile(vector<string> &movies);
MovieList* LoadMovies();

enum MovieSortOrder
{
BY_YEAR = 0,
BY_NAME = 1,
BY_VOTES = 2
};

int main()
{
LoadMovies();

MovieList *movies = LoadMovies();
//movies->MovieList::PrintAll();

// // test methods for the Movie and MovieList classes
//PrintAllMoviesMadeInYear(movies, 1984);
//PrintAllMoviesWithStartLetter(movies, 'B');
//PrintAllTopNMovies(movies, 5);

//delete movies;
return 0;
}

MovieList* LoadMovies()
{
vector<string> movies;
ReadMovieFile(movies);
MovieList ml = MovieList(movies.size());

string name;
int year;
double rating;
int votes;

for (int i = 0; i < movies.size(); i++)
{
istringstream input_string(movies[i]);
getline(input_string, name, '\t');
input_string >> year >> rating >> votes;
Movie movie (name, year, votes, rating);
ml.Add(movie);
}
ml.PrintAll();
}

void ReadMovieFile(vector<string> &movies)
{
ifstream instream;
instream.open("imdbtop250.txt");
if (instream.fail())
{
cout << "Error opening imdbtop250.txt" << endl;
exit(1);
}


while (!instream.eof())
{
string movie;
getline(instream, movie);
movies.push_back(movie);
}

instream.close();
}

当我在主函数中使用 MovieList::PrintAll 时,我的函数就崩溃了,而当我将它放在 LoadMovies 函数中时,它会在崩溃前错误地读取和添加数据。列表的大小为 251,应用程序将只读取相同的数据 251 次。

最佳答案

你有一个两部分的问题:

1:正如 Brad S 所说,您的函数不返回任何内容。这是一个禁忌。

MovieList* LoadMovies()
{
MovieList ml = MovieList(movies.size());
// Your function returns a pointer to a MovieList, so...
return &ml;
}

因此,问题 #2 是您要返回一个指针,指向您在函数的堆栈上创建的内容。当您尝试在您的函数之外访问它时,您将遇到未定义的行为。

选项 1:

 MovieList* ml = new MovieList( movies.size() );
return ml;

您现在需要在完成后删除 ml。

选项 2:更改您的函数以返回非指针...然后您就没有管理内存的麻烦。

编辑:试试这个

int main()
{
// Don't need this
// LoadMovies();

MovieList *movies = LoadMovies();

// Uncommented this
delete movies;
return 0;
}

MovieList* LoadMovies()
{
vector<string> movies;
ReadMovieFile(movies);
// CHANGE
MovieList* ml = new MovieList(movies.size());
// CHANGE

string name;
int year;
double rating;
int votes;

for (int i = 0; i < movies.size(); i++)
{
istringstream input_string(movies[i]);
getline(input_string, name, '\t');
input_string >> year >> rating >> votes;
Movie movie (name, year, votes, rating);
ml.Add(movie);
}
ml.PrintAll();
// CHANGE
return ml;
}

关于C++函数导致应用程序崩溃并且无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27325269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com