Archived

This topic is now archived and is closed to further replies.

vector list problem

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

Hello, I have problems with the std::vector class:
  
CreateWnd()
{

...

CGUIWnd wnd = CGUIWnd(idwnd,x,y,w,h);
m_aWnds.push_back(wnd);

CGUIButton button = CGUIButton(idbutton,m_aWnds.back(),x,y,w,h);
m_aButtons.push_back( button );

...
}
  
What happens? I create a window and than a button. The window are registered as the parent for the button. CGUIButton is derived from CGUIWnd. In the CGUIButton class I have a pointer to a CGUIWnd (that is the parent of the button). This works fine first time the function is called but when the second window is created the first button (the button on the window) get crap values! Something happens when the vector object m_aButtons inserts a new element to the list. I think it is because of the CGUIWnd pointer in the CGUIButton class. When a new element is inserted in m_aWnds the pointer in CGUIButton class isn´t valid anymore. Please help! Zeblar Nagrim, Lord of Chaos

Share this post


Link to post
Share on other sites
With the limitted code you''ve put here, it appears that your problem is in the definition of your vector. It looks like you''re defining a vector of objects, not pointers, and pushing-in derived class objects. This will not work. In order to use polymorphism in STL containers you have to use pointers (or smart pointers), not objects.

Share this post


Link to post
Share on other sites