Kleiver Marcano
Kleiver Marcano

Reputation: 37

How to add an object to a static vector within a class

I'm making a simple program in c++ that let the user to enter a movie with his name, category and rating. I created two classes, the first class is the class Movie. This class has the constructor to instantiate a new Movie object with the attributes i said before(name, category, rating). Then i have a second class called Movie. In this class, i want to add every Movie object to a static vector and then do operations with them.

The problem is that when i try to add a Movie object to the vector, it seems to skip the part off adding it. in other method findMovie(), when i go throught this vector, it return that is empty.

So how can i create a static vector correctly if is not correct now, and , how to add Movie objects to this vector.

Here is the code:

Movie Hpp:

    #ifndef MOVIE_HPP_
#define MOVIE_HPP_
#include <string>
#include <vector>
#include <iostream>
class Movie{
private:
    std::string name;
    std::string rating;
    std::string category;
    int id;
    static int MoviesCreated;
    friend class Movies;
public:
    //Constructor
    Movie(std::string, std::string, std::string, int, int);
    Movie(std::string, std::string, std::string, int);
    Movie();
    //Copy constructor
    Movie(const Movie &);

    //Destructor
    ~Movie(void);
    //Methods
    std::string getName(void) const;
    std::string getRating(void) const;
    std::string getCategory(void) const;
    int getLike(void) const;
    int getId(void) const;
    static int getMoviesCreated(void);
};

Movies.hpp:

    #ifndef MOVIES_HPP_
#define MOVIES_HPP_
#include <string>
#include <vector>
#include "Movie.hpp"

class Movies{
private:
    friend class Movie;
    static std::vector <Movie> moviesCollection;
public:
    void addMovie(Movie);
    void deleteMovie(std::string);
    Movie * findMovie(std::string);
    static std::vector <Movie> getCollection();
};


#endif

Movies.cpp:

     #include "Movies.hpp"

        std::vector <Movie> Movies::moviesCollection;

        std::vector <Movie> Movies::getCollection(void){
            return moviesCollection;
        }

        void Movies::addMovie(Movie m){
    Movies::getCollection().push_back(m);
}

The method addMovie, doesn't add a movie.

this is how i implement it in main inside a function.

    void enterMovie(void){
    string name, rating, category;
    int like,c;
    cout << "Enter the name: ";
    getline(cin, name);
    cout << "Enter the rating: ";
    getline(cin, rating);
    cout << "Enter the category: ";
    getline(cin, category);
    cout << "Enter a calification between 1-10 to rate this movie: ";
    cin >> like;
    if((c= getchar()) =='\n') c+=c;
    Movie Mov(name, rating, category, like);
    cout << "Movie entered: \n";
    display(Mov);
    collections.addMovie(Mov);
}

"collections" is a Movies object that i made global.

Movies collections;
int main(){

The problem required the use of the two classes together.

i'm learning c++ recently, so every improvement to this code will be very helpful

Upvotes: 1

Views: 484

Answers (1)

1201ProgramAlarm
1201ProgramAlarm

Reputation: 32732

getCollection returns a copy of the current collection. Since you want to modify the collection, you should return a reference:

static std::vector<Movie> &getCollection();

Note the addition of the & after the type to change it from a value to a reference.

Upvotes: 4

Related Questions