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.

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

Share on other sites
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

(*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 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).

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.

1. 1
2. 2
3. 3
Rutin
23
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• Forum Statistics

• Total Topics
633653
• Total Posts
3013162
×

Important Information

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!