Avoiding “Pure Virtual Function Call” in Derived C

2019-07-01 09:14发布

I'm reasonably new to C++ so I'd like to apologize if the level of this question is a little below the usual standards here -

I'm trying to get several classes to inherit from a base class which has a virtual function definition, then I'd like to create an array of MainClass* which can include all of the derived classes in order to output the derived + defined virtual function.

I receive the Error "R6025: pure virtual function call" - I don't know why, I was assuming that - when called - the updated definition of that function from the child classes was used.

Here's my code:

Base:

class mitarbeiter
{
public:
    mitarbeiter(string name, int nummer);
    ~mitarbeiter(void);
    virtual void einkommen() = 0;
protected:
    string name;
    int nummer;

};

Derived1:

#include "mitarbeiter.h"
class lohnempfaenger : public mitarbeiter
{
public:
    lohnempfaenger(int stundenlohn, int anzahlStunden, string name, int nummer);
    ~lohnempfaenger(void);
    void einkommen();
private:
    int stundenlohn;
    int anzahlStunden;
};

Derived 2:

#include "mitarbeiter.h"
#include <string>
class angestellter : public mitarbeiter
{
public:
    angestellter(int gehalt, string name, int nummer);
    ~angestellter(void);
    void einkommen();
private:
    int gehalt;
};

Implementation of "einkommen()" in Derived 1:

void lohnempfaenger::einkommen()
{
    cout << "Lohnempfaenger, Name: " << this->name << ", Gesamtlohn: " << this->stundenlohn*this->anzahlStunden << "\n";
}

Implementation of "einkommen()" in Derived 2:

void angestellter::einkommen()
{
    cout << "Angestellter, Name: " << this->name << ", Einkommen: " << this->gehalt << "\n";
}

Array-Declaration in main method:

mitarbeiter* mitPtr[5];
mitPtr[0] = &angestellter(/*values...*/);
//error
mitPtr[0]->einkommen();

Constructors:

mitarbeiter::mitarbeiter(string name, int nummer)
{
    this->name = name;
    this->nummer = nummer;
}

angestellter::angestellter(int gehalt, string name, int nummer):mitarbeiter(name, nummer)
{
    this->gehalt = gehalt;
}

lohnempfaenger::lohnempfaenger(int stundenlohn, int anzahlStunden, string name, int nummer):mitarbeiter(name, nummer)
{
    this->stundenlohn = stundenlohn;
    this->anzahlStunden = anzahlStunden;
}

Thanks a lot!

2条回答
我命由我不由天
2楼-- · 2019-07-01 09:33

The error message, which I'm guessing you see at run time, is saying that mitarbeiter::einkommen() is being called.

Your main code claims to be allocating a new angestellter, whose code you did not provide, but there are several possible places that the call could be hiding.

If mitarbeiter::mitarbeiter calls einkommen(), you will get mitarbeiter::einkommen() no matter what type you specified to new. This seems the most likely place.

The other possibilities are explicit calls to the fully qualified mitarbeiter::einkommen() in either angestellter::angestellter() or angestellter::einkommen().

查看更多
姐就是有狂的资本
3楼-- · 2019-07-01 09:37

Now you've shown us how you actually initialise the pointer, here is the problem:

mitPtr[0] = &angestellter(/*values...*/);

That points to a temporary object, which is destroyed at the end of the statement. Using the pointer afterwards will give undefined behaviour.

You'll need a non-temporary variable to point to:

angestellter a(/*values...*/);
mitPtr[0] = &a;

or perhaps a dynamically allocated object:

mitPtr[0] = new angestellter(...);

In that case, don't forget to delete it; and don't forget to give the base class a virtual destructor so it can be deleted. You might consider using a smart pointer to delete it for you; std::unique_ptr would be ideal.

查看更多
登录 后发表回答