对于我的任务,我仍然有点卡在另一部分。C++ MovieList数组和指针
这里有什么提示,询问:
现在你可以修改LoadMovies函数创建一个MovieList 对象并添加每个电影的反对它。函数 LoadMovies应该返回一个指向MovieList对象的指针。这意味着 您需要动态地在堆上创建MovieList对象。
变化的主要功能和所述返回MovieList指针存储在一个变量 。要测试一切是否按预期工作,您可以使用MovieList对象的PrintAll函数 。
这是到目前为止我的代码:
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();
^
在C++“阵列”是不是动态的(他们创建后不改变)。他们可以*动态分配*。 – crashmstr 2014-12-05 18:38:53
您的指示说要制作Movie对象的数组,但您已创建了一个int数组。 – 2014-12-05 18:42:31
嘿斯科特,你能向我解释你的意思吗?所以我应该做电影=新电影[movies_size]而不是* int电影应该是*电影电影? – andayn 2014-12-05 19:15:27