對於我的任務,我仍然有點卡在另一部分。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