简体   繁体   中英

c++ interface throws "undefined reference to `vtable for 'interface'" error

I am building a sound generator program in Java and I am trying to port it to c++

I can easily do this in Java, but I am new to c++

In c++ I have an interface called iSamplePlayer and this is the iSample.h file:

class ISamplePlayer {
public:
    
    virtual double GetFrequency()
    
    virtual void SetFrequencyAtZero(double frequency)
    
    virtual void SetSampleRate(uint32_t sampleRate)
    
    virtual void SetAmplitude(double amplitude)
    
    //virtual void SetAdsrEnvelope(AdsrEnvelope adsrEnvelope)
    
    virtual void SetOffset(double offset)
    
    virtual void AddToFrequency(double offset)
    
    virtual void SubtractFromFrequency(double offset)

    virtual double GenerateWaveformPoint() = 0;
};

a base class called AWaveform, this is the AWaveform.h file:

class AWaveform : public ISamplePlayer{
public:

    // constants
    const uint32_t SAMPLE_RATE = 44100;

    // constructors
    AWaveform(double frequency, uint32_t sampleRate);
    AWaveform(double frequency);
    AWaveform(const AWaveform& orig);
    virtual ~AWaveform();
    
    // getters setter and other methods are here

    // generateWaveForm method
    virtual double GenerateWaveformPoint() override = 0;

   
    
private:

    // instance variables
    double amplitude; // volume
    double frequency; // pitch
    double point; // current sample
    uint32_t sampleRate; // rate of samples per second
    double holdFrequency; // hold frequency till it can be changed.
    double waveLength; // length of one waveform in samples
    double waveSampleIndex; // keeps track of the waveLength
};

and Finally in the class SineGenerator, I have the GenerateWaveformPoint() function in the SineGenerator.h that actualy overrides with code:

class SineGenerator : AWaveform{
public:
    
    // constants
    const double DEF_PEAK_PERCENTAGE = 0.5;
    const double DEF_HORIZONTAL_OFFSET = 0;
    
    // constructors
    SineGenerator(double frequency, double peakPercentage, double offset);
    SineGenerator(double frequency, double peakPercentage);
    SineGenerator(double frequency);
    SineGenerator(const SineGenerator& orig);
    virtual ~SineGenerator();
    
    // methods
    double GenerateWaveformPoint() override {

        // check frequency
        CheckFrequency();

        // genorate point.
        DrawSinePoint();

        // checks if waveSampleIndex is equal to wavelength
        CheckWaveLength();
        
        // apply ADSR amplitude
        //if (getAdsrEnvelope() != null) {
        //    setPointVolume(getAdsrEnvelope().getADSRvolume());
        //}
        
        // set volume
        SetPointVolume(GetAmplitude());

        // return the sample data
        return GetPoint();
    }
protected:
    void DrawSinePoint() 
    {
        // code goes here
    }
private:
    // instance variables
    double peakPercentage;
    double smallWaveFrequency;      // used for != 50% waves
    double smallWaveIndex;          // index of smallwave
    double smallWaveLength;         // use to hold small wave length for != 50%
    double holdIndex;               // used to keep track of hold time
    double holdLength;              // use to hold hold length for != 50%
    double updatePeakPercentage;    // holds the update value

};

It gives this error "undefined reference to `vtable for ISamplePlayer'".

How do I properly make a virtual function that overrides an interface's virtual function?

OK, I was a Dummy. I forgot to add = 0; to the other functions in the interface. Everyone that is learning c++ and already knows java or c#, all functions that you intend to be abstract need to be virtual and end in = 0; These are called pure virtual functions. Learn from my mistake.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM