user393964
user393964

Reputation:

Inheritance implementation not working

I have an interface called Generator that looks like this:

class Generator{
public:
    virtual float getSample(Note &note)=0;
};

And my Synth class is implementing it, like this:

class Synth : public Generator{
public:
    virtual float getSample(Note &note);
};

float Synth::getSample(Note &note){
    return 0.5;
}

I'm trying to call the getSample method from my Note class (which has a generator member)

class Note : public Playable{
public:
    Generator *generator;
    virtual float getValue();
};

float Note::getValue(){
    float sample = generator->getSample(*this); // gets stuck here
    return sample;
}

When I try to run, it gets stuck on the marked line in the code above. The problem is that I'm not getting a very clear error message. This is what I can see once it stops:

enter image description here enter image description here

Upvotes: 1

Views: 55

Answers (1)

Luchian Grigore
Luchian Grigore

Reputation: 258548

It seems like you never initialized the member Note::generator, so calling a function on it is undefined behavior.

Try, as a test:

float Note::getValue(){
    generator = new Synth;
    float sample = generator->getSample(*this); // gets stuck here
    return sample;
}

If it works, go back and review your logic. Use a std::unique_ptr<Generator> instead of a raw pointer. Create a constructor for Node. Initialize the pointer there.

Upvotes: 3

Related Questions