Jump to content
  • Advertisement
Sign in to follow this  
chondee

Calling member function from vector of std::unique_ptrs

This topic is 2499 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

<SOLVED>

Hi,

I am trying to change the way I handle particles and collections of objects, from vector<actual_objects> to vector<unique_ptr<object>>
My problem is that this way no matter what I'm doing I can't access the member function of the object that is pointed to from the unique_ptr.

Here is how I have the vectors:

vector<unique_ptr<Star>> v_stars;
vector<unique_ptr<Star>>::iterator v_stars_it;


Here is how I populate the vector:

v_stars.push_back(std::unique_ptr<Star> (new Star(SCREEN_WIDTH, randmY, randmType, 1, 1)));


Here is how I am trying to loop through all and call Star.show():

for (v_stars_it = v_stars.begin(); v_stars_it != v_stars.end(); v_stars_it++){
(*v_stars_it)->show();
}


I am probably doing something wrong with the pointer, I tried calling it so many ways, it never works, I get compile errors that unique_ptr<_Type> doesn't have show member function, even though what I believe is that I am trying to ask the member function of what unique_ptr points to, not the unique_ptr itself.

EDIT: actually even for the declaration of the vector (first code quote) I get this error:

Error 14 error C2664: 'bool (Star)' : cannot convert parameter 1 from 'std::unique_ptr<_Ty>' to 'Star' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\algorithm 1839 1 SFML2.0_Blank
What am I doing wrong? (This is the first time I attempted to use smart pointers)

EDIT2: Alright, there is nothing wrong with the code above, I didn't change a line of code for the new implementation and that was the reason for the compiler errors


Please help me out if you can smile.png
Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
Hidden
(*v_star_it)-> referes to the unique_ptr<star>, not the star itself. You need to dereference it once more or access the content of the unique_ptr however it w

Share this post


Link to post
(*v_star_it)-> referes to the unique_ptr<star>, not the star itself. You need to dereference it once more or access the content of the unique_ptr however it works (don't know myself).

Share this post


Link to post
Share on other sites

(*v_star_it)-> referes to the unique_ptr<star>, not the star itself. You need to dereference it once more or access the content of the unique_ptr however it works (don't know myself).


Thanks for your comment.
Yes, (*v_star_it) refers to unique_ptr<star>, but since it is used with -> it doesn't need to be dereferenced anymore. Either way, the problem got solved, it was another mistake.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!