Skip to main content
gamedev.net
🔒 Locked

Pointer and Vector problems

Started by utilae 2 replies 700+ views
Original Post
utilae
utilae
Hi, I am using C++ and DirectX9 and Fmod. I am trying to set a pointer to point to an element in a vector. The element is a random directly accessed element.

////.H
struct STREAMMUSIC
{
	//filepath of music
	string m_sFilename;
	//handle to a stream (music)
	FSOUND_STREAM *m_streMusicHandle;
	//operator to sort music struct
	bool operator<(const STREAMMUSIC &Music) const {return m_sFilename<Music.m_sFilename;}
};

//list of all loaded music
static vector <STREAMMUSIC> m_lstLoadedMusicList;

//current music playing
STREAMMUSIC *m_pCurrentStreamMusic;

////.CPP
//set current music
vector<STREAMMUSIC>::iterator itLoadedMusic=m_lstLoadedMusicList[int((m_lstLoadedMusicList.size()+1)*rand()/(RAND_MAX+1.0))];
m_pCurrentStreamMusic=&(*itLoadedMusic);

I get these compile errors: Sound.cpp(262) : error C2440: 'initializing' : cannot convert from 'std::allocator<_Ty>::value_type' to 'std::vector<_Ty>::iterator' with [ _Ty=CSoundManager::STREAMMUSIC ] and [ _Ty=CSoundManager::STREAMMUSIC ] No constructor could take the source type, or constructor overload resolution was ambiguous Anyone able to help?
HTML5, iOS and Android Game Development using Corona SDK and moai SDK
NumberXaero
NumberXaero
m_lstLoadedMusicList is a vector of streammusic "structs" not a vector of "iterators" so

m_lstLoadedMusicList[int((m_lstLoadedMusicList.size()+1)*rand()/(RAND_MAX+1.0))];

will index to and return a reference to a stream music struct, which your trying to store in an iterator. I think what your trying for is something like

m_pCurrentStreamMusic = &m_lstLoadedMusicList[int((m_lstLoadedMusicList.size()+1)*rand()/(RAND_MAX+1.0))];

but i would suggest storing pointers to streammusic structs in the vector (allocated on heap, dont forget to loop the vector and delete em when finished)
then just do

m_pCurrentStreamMusic = m_lstLoadedMusicList[int((m_lstLoadedMusicList.size()+1)*rand()/(RAND_MAX+1.0))];

which will return a random pointer to a struct
utilae
utilae
Thanks that helped.

Quote:
Original post by NumberXaero
but i would suggest storing pointers to streammusic structs in the vector (allocated on heap, dont forget to loop the vector and delete em when finished)
then just do

m_pCurrentStreamMusic = m_lstLoadedMusicList[int((m_lstLoadedMusicList.size()+1)*rand()/(RAND_MAX+1.0))];

which will return a random pointer to a struct

Would storing pointers be worth it just to not have to use the '&'.
HTML5, iOS and Android Game Development using Corona SDK and moai SDK
snk_kid
snk_kid
Quote:
Original post by utilae
Quote:
Original post by NumberXaero
but i would suggest storing pointers to streammusic structs in the vector (allocated on heap, dont forget to loop the vector and delete em when finished)
then just do

m_pCurrentStreamMusic = m_lstLoadedMusicList[int((m_lstLoadedMusicList.size()+1)*rand()/(RAND_MAX+1.0))];

which will return a random pointer to a struct

Would storing pointers be worth it just to not have to use the '&'.


I don't agree with NumberXaero at all, that structure is small and it will be more efficent to store by value than reference. Elements are already allocated on the heap (by default), storing pointers means another unnecessary level of indirection and instead of having contingous chunk of elements you'll have a contiguous chunk of pointers that refer to most likely non contiguous elements.

And if you really do find performance issues later then write/use a custom allocator instead, all standard library containers are parameterized by allocator type.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.