2014-12-05 181 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