gpt4 book ai didi

C++ MovieList 数组和指针

转载 作者:太空宇宙 更新时间:2023-11-04 13:42:20 25 4
gpt4 key购买 nike

我仍然有点卡在作业的另一部分上。

这里是提示询问的内容:

Now you can modify the LoadMovies function to create a MovieList object and add each of the Movie objects to it. The function LoadMovies should return a pointer to the MovieList object. That means you need to create the MovieList object dynamically and on the heap.

Change the main function and store the returned MovieList pointer in a variable. To test if everything works as expected, you can use the PrintAll function of the MovieList object.

到目前为止,这是我的代码:

class MovieList {

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

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

~MovieList() {
delete [] movies;
}

int Length() {
return movie_count;
}

bool IsFull() {
return movie_count == movies_size;
}

void Add(Movie const& m)
{
if (IsFull())
{
cout << "Cannot add movie, list is full" << endl;
return;
}

++last_movie_index;
movies[last_movie_index] = m;
}

void PrintAll() {
for (int i = 0; i < movie_count; i++) {
movies[last_movie_index].PrintMovie();
}
}

};

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

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

int main()
{
LoadMovies();

// TODO:
// You need to implement the Movie and MovieList classes and
// the methods below so that the program will produce
// the output described in the assignment.
//
// Once you have implemented everything, you should be able
// to simply uncomment the code below and run the program.

MovieList *movies = LoadMovies();

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

//delete movies;
return 0;
}

void LoadMovies()
{
vector<string> movies;
ReadMovieFile(movies);

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

现在我卡在了教授让我修改提示中的 LoadMovies 并将其变成指针的地方。我在画空白。同样出于某种原因,如果我尝试编译它会说:

C:\Users\Andy\Documents\C++ Homework\MovieStatisticsProgram\MovieStatsProgram.cpp:163: error: void value not ignored as it ought to be
MovieList *movies = LoadMovies();
^

最佳答案

你的构造函数的顺序是错误的

MovieList(int size) {
movies = new int[movies_size]; // movies_size hasn't been initialized yet!
movies_size = size;
last_movie_index = -1;
}

应该是

MovieList(int size)
: movies_size{size}, movies{new int[size]}, last_movie_index{0}
{}

虽然@ScottMcP-MVP 指出你的数组应该是

Movie* movie;

所以你的构造函数是

MovieList(int size)
: movies_size{size}, movies{new Movie[size]}, last_movie_index{0}
{}

开始使用其余功能的一些建议

Length 函数将从 last_movie_index 返回当前使用的数量。

IsFull 将检查 last_movie_index == movies_size - 1

Add 将需要使用 last_movie_index 来确定数组中的哪个元素来存储电影。

PrintAll 必须从 [0] 迭代到 [movie_count] 并打印出每个元素。

您的Add 函数看起来像

void MovieList::Add(Movie const& m)
{
if (IsFull())
{
std::cout << "Cannot add movie, list is full" << std::endl;
return;
}

movies[last_movie_index] = m; // assigns a copy to your array
++last_movie_index; // Increment the movie index
}

关于C++ MovieList 数组和指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27322490/

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