2014-12-05 180 views
1

对于我的任务,我仍然有点卡在另一部分。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(); 
            ^
+0

在C++“阵列”是不是动态的(他们创建后不改变)。他们可以*动态分配*。 – crashmstr 2014-12-05 18:38:53

+0

您的指示说要制作Movie对象的数组,但您已创建了一个int数组。 – 2014-12-05 18:42:31

+0

嘿斯科特,你能向我解释你的意思吗?所以我应该做电影=新电影[movies_size]而不是* int电影应该是*电影电影? – andayn 2014-12-05 19:15:27

回答

0

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

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; 

所以,你constuctor将

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 
} 
+0

感谢您的帮助,我有几个问题......我有点卡在添加需要插入数组中的下一个可用元素的算法,正如斯科特先前说的,因为我的教授是要求一个Movie对象的数组,它会是movies = new Movie [movie_size]而不是? – andayn 2014-12-05 19:09:59

+0

@andayn是的,你会有一个'Movie'数组,你必须定义你的'Movie'类。请参阅我的关于“添加”功能的编辑。 – CoryKramer 2014-12-05 19:21:48

+0

对不起,如此困扰你,但你能重读我的主题,并帮助我解决我的新问题吗?使用指针正在杀死我。 – andayn 2014-12-05 19:48:30