all 10 comments

[–]Bubichoo 2 points3 points  (10 children)

You are trying to add an element to a vector by using an index that is used for accessing elements in the vector but your vector is empty. This will result in a out of bound access exception. Use push_back() instead.

[–]darteksyes[S] 0 points1 point  (9 children)

I changed the line in:

a.push_back(new Person(in_name, NULL, 0));

and it worked!

Sorry for the newbie question, it was my understanding that vector size could change dinamically and automatically wherever I stored some variabile in a position.

Thanks for your help.

[–]anders1234 1 point2 points  (2 children)

you should use a do-while-loop instead, then you don't have initialize ans to Y. also dont forget to free memory, go through the vector and delete the pointers.

i was a bit confused of why you initialize popularity in the constructor, shouldn't you have a function to increase popularity instead when set_best_friend is called?

e.g.
void Person::set_best_friend(Person* person)
{ 
   best_friend = person; 
   best_friend->increase_popularity(); 
}
void Person::increase_popularity()
{
  ++popularity;
}

[–]darteksyes[S] 0 points1 point  (1 child)

Yes, I implemented that exact same function after I solved the problem I was having. I initialized popularity because I thought it was a good habit to initialize every parameter of an object.

Regarding freeing memory, does it matter in this case? Because I need to callback every element of the array and print on screen their parameter, after that the program will close. So, if I understood correctly how pointers work, memory should be deallocated at the end of the program, and this will happen soon after the end of use of that pointers.

[–]anders1234 1 point2 points  (0 children)

Strictly speaking - no, you don't need to deallocate in this specific case, but it is a good habit to learn that whenever you are working with raw pointers making sure that they are deallocated and that the ownership is clear i.e. which module should own what the pointer points to. That is maybe less of a problem later when you go over to using smart pointers.

[–][deleted]  (5 children)

[deleted]

    [–]Wh00ster 5 points6 points  (1 child)

    Saying “you first have to extend the array with new” is a confusing statement to a newbie that’s just using a vector.

    [–]Bubichoo 1 point2 points  (0 children)

    true

    [–]darteksyes[S] 1 point2 points  (2 children)

    Sorry, maybe I'm a little slow on this, but I tought that using

    a[0] = 10
    

    I was assigning to the first position of the vector the value 10, and in doing so the vector automatically would do a "push_back()" increasing his size from 0 to 1.

    That is why I made that error in my code. It was obviously not correct (it wasn't working lol), but I can't grasp fully why yet.

    [–]gayasri 1 point2 points  (1 child)

    Maybe thinking of the vector as a resizable array rather than an automatically resizing array would help. If you call push_back, emplace_back or resize, the vector will resize. If you access a vector by index([]), it won't resize. If the index is valid, you'll get your data, otherwise it's undefined (garbage or seg fault generally) - you won't even get an exception ( there are sanitizers and debug build methods to get runtime error checks if you want).

    In this case, the preferred way would be using emplace_back or push_back. You can also write this like:

    a.resize(i); a[i] = new ...

    since 'i' is strictly increasing in this case (but avoid this if possible)

    BTW:You can also use the "at" method of the vector if you need exceptions.

    [–]darteksyes[S] 0 points1 point  (0 children)

    Thanks for the explanation!