Sign in to follow this  
ErUs

vector / array help

Recommended Posts

im trying to create a dynamic list of things to render for a gui class thingy. this is what i want to do: Basicly, to get the gui to add some text one would do gui->AddText( "bleh" , 2 , 2 ); and this would return a gui element id of some sort. the gui class would add this to the list of things tobe rendered. Then when i want it to go away i type gui->remove( <what was returned from AddText> ); i would like to have things tobe rendered as a vector but i cant keep track of a specific item in the array after i add/remove elements from the array. please help

Share this post


Link to post
Share on other sites
It sounds like you should be using a map. When you generate the GUI element, you would then come up with a token of some sort, add the element into the map with the token return the token. Then when removing search the map for the token. Alternately you could potentially use a pointer to the GUI element as the token, in which case you can use a set with the pointer.

Share this post


Link to post
Share on other sites
got a little problem - cant figure it out.



//DEFINED AS CLASS MEMBER
std::map<int, irr::gui::IGUIElement*> guie;

//PROBLEM CODE

int CUI::AddText( std::wstring text, int x, int x1, int y, int y1 )
{

IGUIElement * guielement =
guienv->addStaticText( text.c_str(), rect<s32>((x*scrsize), (y*scrsize), (x1*scrsize), (y1*scrsize) ),
false, true, (IGUIElement *) NULL, -1, false );

if( !guielement )
return -1;

return guie.insert( guielement ); //ERROR HERE

}

void CUI::RemoveElement( int id )
{

guie[id]->drop();
guie.erase( id );

}




and the error is:


error C2664: 'std::_Tree<_Traits>::_Pairib std::_Tree<_Traits>::insert(const std::_Tree<_Traits>::value_type &)' : cannot convert parameter 1 from 'irr::gui::IGUIElement *' to 'const std::_Tree<_Traits>::value_type &'
with
[
_Traits=std::_Tmap_traits<int,irr::gui::IGUIElement *,std::less<int>,std::allocator<std::pair<const int,irr::gui::IGUIElement *>>,false>
]
and
[
_Traits=std::_Tmap_traits<int,irr::gui::IGUIElement *,std::less<int>,std::allocator<std::pair<const int,irr::gui::IGUIElement *>>,false>
]




what am i doing wrong?

Share this post


Link to post
Share on other sites
You can't just insert a pointer and expect map to give you an int. You need to figure out a way to generate the int yourself (or switch to std::set and just store the pointer).

Share this post


Link to post
Share on other sites
When inserting into a map, you have to insert both the key and the value as a std::pair. Furthermore, std::map::insert() doesn't return an int, it returns a std::pair that contains an iterator to where the item was inserted, and a bool indicates the success of the insertion (which can fail if there is already an entry with the same key).

You'll need to generate you're ID integer yourself, and then use it as follows to do the insert:
guie.insert( std::make_pair(id,guielement) );
return id;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this