vector / array help

Started by
5 comments, last by ErUs 18 years, 5 months ago
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
-www.freewebs.com/tm1rbrt -> check out my gameboy emulator ( worklog updated regularly )
Advertisement
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.
just what i need :)

thanks alot
-www.freewebs.com/tm1rbrt -> check out my gameboy emulator ( worklog updated regularly )
got a little problem - cant figure it out.

//DEFINED AS CLASS MEMBERstd::map<int, irr::gui::IGUIElement*> guie;//PROBLEM CODEint 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?

-www.freewebs.com/tm1rbrt -> check out my gameboy emulator ( worklog updated regularly )
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).
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;

"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
edit: wait...

just realised that im going to have to keep an index of everything myself, which kinda defeats the point of this...

thanks anyway guys

-www.freewebs.com/tm1rbrt -> check out my gameboy emulator ( worklog updated regularly )

This topic is closed to new replies.

Advertisement