-2

I'm trying to use virtual methods in C++.

I have an objects hierarchy:

class Action {
public:
    Action() { }

    virtual void action() = 0;
}

class CubeAction : public Action {
public:
    CubeAction() { }

    void action() {
        glutSolidCube(1);
    }
};

And I have a container for objects of the base class.

class ActionContainer {
private:
    std::vector<Action *> actions;

public:
    void add(Action &action) { actions.push_back(&action); }

    void doActions() {
        for (auto a : actions) {
            a->action();
        }
    }
};

And when I try to doActions:

ActionContainter actions();

CubeAction cubeAction();
actions.add(cubeAction);

actions.doActions();

I get pure virtual method called error.

I used the Calling a virtual function on a vector of base classes solution, but error still happened.


UPD: sorry, that is my real code: Github. Some bug in Action.h file, I think.

Community
  • 1
  • 1
wlad031
  • 107
  • 12
  • Since the code has syntax errors it's not **real code**. Please post a complete but minimal example that readers can try. – Cheers and hth. - Alf May 25 '16 at 10:26
  • @JeremyThompson: The link is good, thanks; the acronym however is silly and counter-productive. Don't assume that people who post questions here will know it. – Cheers and hth. - Alf May 25 '16 at 10:28
  • http://meta.stackexchange.com/questions/92060/add-data-se-style-magic-links-to-comments - I find them helpful – Jeremy Thompson May 25 '16 at 10:29
  • 3
    @JeremyThompson: Mainly I stay away from meta. Those discussions mostly draw conformists (idiots) and SO moderators (people who have campaigned to get themselves elected to wield more powers, i.e. politicians). Meta is OK for reporting issues: on meta the quality of an issue is generally proportional to the number of downvotes from the conformists; the more downvotes, the more likely it's a real issue that ideally would be fixed (but it won't be). – Cheers and hth. - Alf May 25 '16 at 10:31
  • Once you fix the obvious compilation errors it works as expected. Post your real code! http://ideone.com/Bd6sMF – Roddy May 25 '16 at 10:33
  • Except syntax errors, the code has no problem. Please give us the real failing code. – Jean-Baptiste Yunès May 25 '16 at 10:34

1 Answers1

1

Your code in github looks like this:

try {
    SphereAction sphereAction(1);
    actions.add(sphereAction);

    CubeAction cubeAction(2);
    actions.add(cubeAction);

} catch (InfoException e) {
    cerr << e.what() << endl;
}

and afterwards, you call display(), which calls ActionContainer::doActions().

In the container, you store a pointer to the argument, but you pass a local variable to actions.add(), which will be out of scope and no longer exist when it is used.

Karsten Koop
  • 2,475
  • 1
  • 18
  • 23
  • Thank you very much! But why two of unused vector led to such consequences? – wlad031 May 25 '16 at 11:47
  • The destructor gets called, making the virtual function pure again. As the objects are on the stack, the memory is still there, even after the lifetime of the objects, until it gets overwritten. – Karsten Koop May 25 '16 at 13:17