Jump to content
  • Advertisement
Sign in to follow this  
eltharynd

accessing vector reference through iterator

This topic is 2126 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

I need to call the member functions of all the Objects of a vector<Object*>*

 

I pass the Pointer to a vector of pointers to Objects to this function but I'm struggling to find a valid way to call the Object::Render() function for every Object stored in the pointed vector......

this was my best guess but it obviously doesn't work....

void Engine::RenderFrame(vector<Object*>* statics)
{
	//clear the back buffer to black
	pDeviceContext -> ClearRenderTargetView(pBackBuffer, D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f));
	
	//3D rendering goes here
	for(vector<Object*>::iterator it = &statics->begin; it != &statics->end; ++it)
	{
		it->Render();
	}

	//switch the back buffer and the front buffer
	pSwapChain -> Present (0,0);
}

any suggestions?

 

Share this post


Link to post
Share on other sites
Advertisement

I would start by clarifying what you mean by "doesn't work". Are you getting compiler errors? Linker errors? Runtime errors?

Share this post


Link to post
Share on other sites
Error 36 error C2276: '&' : illegal operation on bound member function expression c:\users\eltharynd\documents\visual studio 2012\projects\space\space\engine.cpp 68 1 Space
Error 37 error C2839: invalid return type 'Object **' for overloaded 'operator ->' c:\users\eltharynd\documents\visual studio 2012\projects\space\space\engine.cpp 70 1 Space
Error 38 error C2039: 'Render' : is not a member of 'std::_Vector_iterator<_Myvec>' c:\users\eltharynd\documents\visual studio 2012\projects\space\space\engine.cpp 70 1 Space
 
they are all related to the fact that i'm dereferencing my object in an incorrect way

Share this post


Link to post
Share on other sites

I'll semplify everything for ease

vector<Object*>* statics;

for(    vector<Object*>::iterator it = &statics->begin; 
        it != &statics->end; 
        ++it)
{
	it->Render();
}

this should be easier to read...

Share this post


Link to post
Share on other sites
The first error is saying you can't use & on statics->begin and statics->end. The reason why is that statics->begin is a bound member function expression, which doesn't have an address. What you probably wanted is statics->begin() and statics->end(). No & and you need the () to actually call the function.

The last two errors say that the return value of operator-> is an Object **, which you can't use ->Render() on. You need to dereference the iterator to get an Object * and then you can use ->Render() on it. So (*it)->Render().

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!