a vector of classes

I was just introduced to this vector type, and i used it to return arrays of objects from functions. The objects have private values that can only be accessed trough functions. Thing is, i can't seem to call those functions like i did when i made arrays of those objects. My question is if i can call them from the vector type, and how if so?
Vectors and arrays are accessed the same way.

1
2
    std::vector<Foo> v = make_some_foos();
    std::cout << v[2].fun() << '\n'; // assuming v.size() > 2 
The matter is still unclear to me. Let me provide you my code maybe it can be of some help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class movie
{
    public:
        movie();
        void create(string n,string s);
        string getname();
        void setname(string n);
        string getdesc();
        void setdesc(string d);

    private:
        string name;
        string desc;

};


This is my movie class. You can notice the values are private and are accessed trough getters, which both return strings.

I have another function that reads from a file and returns a vector of movie objects (* the rdmovies() function from the repository class). This is where i try to make use of that:

1
2
3
4
5
6
7
8
9
void controller::listmv()
{
    vector <movie> movies;
    movies = rp.rdmovies();  // * the said function is used here
    for (int i=0; i<=movies.size(); i++)
    {
        cout << movies[i].getname() << movies[i].getdesc() << endl; // the error line
    }
}


This seems fine in my head, but obviously, c++ doesn't agree. Hope you understand what i'm trying to do here and give me the piece of information i'm missing
The one error I am seeing is that the loop runs off the end of the vector. It should be for (int i=0; i<movies.size(); i++)
It magically worked now, i think i mistyped something. Runs now but the output is a strange address. Back to running in depth bug hunting -_-. Thank you for your time!
Topic archived. No new replies allowed.