Bug on pointer vector::push_back
Feb 27, 2014 at 12:11pm UTC
Hello everyone,
Today I get a really weird error from this code. It compiles perfectly, but crashes at the push_back line :(
1 2 3 4 5 6 7 8 9 10 11
class MenuNav
{
public :
MenuNav();
~MenuNav();
void addButton(MenuButton* button);
private :
std::vector<MenuButton*> m_buttons;
};
That was the header and here is the implementation
1 2 3 4 5 6 7
void MenuNav::addButton(MenuButton* button)
{
//std::cerr << "but=" << button << " " << !!button; //works when I test
m_buttons.push_back(button); //crash here
//std::cerr << "Crashes b4" ; //doesn't display
}
When I do this:
m_nav->addButton(new MenuButton(the constructor parameters));//bug on this line boys
The program just crashes when I ncomment it, for no obvious reason :(
If someone know what's going on the let me know :)
Feb 27, 2014 at 12:25pm UTC
Feb 27, 2014 at 1:12pm UTC
i see that m_nav is a pointer. I suspect that it does not point to the valid MenuNav object. In that case you will ge symptoms you described.
Feb 27, 2014 at 1:42pm UTC
The pointer m_nav has to be properly defined by defining the object with a valid initialized value and then call the method addButton() function.
Topic archived. No new replies allowed.