分配抽象类类型为error的对象

allocating an object of abstract class type error

本文关键字:对象 error 抽象类 类型 分配      更新时间:2023-10-16

你好,我得到以下错误,我真的不知道为什么。

class InteSiVis: public ofBaseApp //{
,  public ofxMidiListener{

当我从ofxmidillistener类继承类inresivis并在主源文件

中得到以下错误时,就会发生这种情况

int main(){

ofSetupOpenGL(1920,1080, OF_WINDOW);            
ofRunApp( new InteSiVis()); // <-------- The error is here Allocating object of type abstract

}

这真的很令人困惑,因为我已经用同样的方式用另一个例子尝试过了,但没有得到这个错误。

class testApp : public ofBaseApp, public ofxMidiListener {
int main(){
    ofSetupOpenGL(640, 480, OF_WINDOW);
    ofRunApp(new testApp());
}

你能给我一个想法,为什么我得到这个错误,我调用类在完全相同的方式。提前谢谢。

///---------------------------------- 编辑InteSiVis.h

class InteSiVis: public ofBaseApp //{
,  public ofxMidiListener{
public:
    InteSiVis() ;
    void setup();
    void update();
    void draw();
    void exit();
    void keyPressed(int key);
    void keyReleased(int key);
    // Make an Array of Particle Systems
    vector<FluidBodySim> mArrayFluidBodySim;
    FluidBodySim        mFluidBodySim       ;   ///< Simulation of fluid and rigid bodies
    int                 mStatusWindow       ;   ///< Identifier for status window
    unsigned            mFrame              ;   ///< Frame counter
    double              mTimeNow            ;   ///< Current virtual time
    int                 mMouseButtons[3]    ;   ///< Mouse buttons pressed
    bool                mInitialized        ;   ///< Whether this application has been initialized
    int                 mScenario           ;   ///< Which scenario is being simulated now
// Scene stuff
    ofEasyCam mEasyCam;
    ofLight light;
// Setting Shader stuff
    ofShader shader;
    ofxPostProcessing post;
// Sound
    float   * lAudioOut; /* outputs */
    float   * rAudioOut;
    float * lAudioIn; /* inputs */
    float * rAudioIn;
    int     initialBufferSize; /* buffer size */
    int     sampleRate;
    double wave,sample,outputs[2];
    maxiSample piano_A1, piano_AS1, piano_B1, piano_C1, piano_CS1, piano_D1, piano_DS1, piano_E1, piano_F1, piano_FS1, piano_G1, piano_GS1;
    vector<maxiPitchStretch<grainPlayerWin>*> stretches;
    maxiPitchStretch<grainPlayerWin> *ts, *ts2, *ts3, *ts4, *ts5;
    int nAverages;
    float *ifftOutput;
    int ifftSize;
//    // Playing the Wav Files
    void audioOut(float *output, int bufferSize, int nChannels);
    double speed, grainLength, rate;
    ofxMaxiFFT fft;
    ofxMaxiFFTOctaveAnalyzer oct;
    int current;
    double pos;

} ;

testApp.h

class testApp : public ofBaseApp, public ofxMidiListener {
public:
    void setup();
    void draw();
    void exit();
    void keyPressed(int key);
    void keyReleased(int key);
    void mouseMoved(int x, int y );
    void mouseDragged(int x, int y, int button);
    void mousePressed(int x, int y, int button);
    void mouseReleased();

    stringstream text;
    vector<ParticleSystem> ps;
    //----------------------Sound---------------------------
    void newMidiMessage(ofxMidiMessage& eventArgs);

    ofxMidiIn midiIn;
    ofxMidiOut midiOut;
    ofxMidiMessage midiMessage;
    void audioOut(float *output, int bufferSize, int nChannnels);
};

//---------------- 虚函数vorticitydistribution.h

class IVorticityDistribution
{
    public:
        virtual Vec3 GetDomainSize( void ) const = 0 ;
        virtual void AssignVorticity( Vec3 & vorticity , const Vec3 & position , const Vec3 & vCenter ) const = 0 ;
} ;
class JetRing : public IVorticityDistribution
{
    public:
        /*! brief Initialize parameters for a vortex ring (using a different formula from the other).
            The vorticity profile resulting from this is such that the induced velocity is in [0,1].
            param fRadiusSlug - radius of central region where velocity is constant
            param fThickness - thickness of vortex ring, i.e. radius of annular core
            param vDirection - vector of ring axis, also vector of propagation
            param fSpeed   - speed of slug
        */
        JetRing( const float & fRadiusSlug , const float & fThickness , const Vec3 & vDirection )
            : mRadiusSlug( fRadiusSlug )
            , mThickness( fThickness )
            , mRadiusOuter( mRadiusSlug + mThickness )
            , mDirection( vDirection )
        {
        }
        virtual Vec3 GetDomainSize( void ) const
        {
            const float boxSideLength   = 2.f * ( mRadiusOuter ) ;    // length of side of virtual cube
            return Vec3( 1.0f , 1.0f , 1.0f ) * boxSideLength ;
        }
        virtual void AssignVorticity( Vec3 & vorticity , const Vec3 & position , const Vec3 & vCenter ) const
        {
} ;

事情是这样的:

class Base
{
        public:
         const std::string SayHi() { return "Hi"; } // a normal non-virtual method            
         virtual std::string GetName() { return ("Base"); } // a normal virtual method
         virtual int GetValue() = 0; // a pure virtual method
}; 

当你像这样声明testApp时class testApp : public Base { ... };:

普通非虚方法在Base内部声明时继承,并且是不可变的。

普通虚方法在Base内部声明时继承,您可以在已经声明时使用它们,或者根据特定目的重新定义它们。

纯虚方法没有定义,父类只说"如果你从我继承,你必须自己实现这些,严格匹配我的原型(我如何定义它们)

如果你不遵守这些规则,你就会得到错误。


现在,您必须确保在ofBaseAppofxMidiListener中没有没有实现到子类中的纯虚拟方法。

既然你声明类testApp不做任何错误,你必须在InteSiVis中有一个来自你忘记实现的父类的纯虚方法。

好吧,你还没有发布足够的信息来确定。

通常当你得到一个消息,编译器无法创建抽象类型的类,而你试图实例化一个继承自某个接口的类,这意味着你没有提供接口指定的纯虚拟方法的实现之一。

在你的testApp的实现中,你是否指定了任何你没有在InteSiVis中指定的方法的覆盖?签名必须完全匹配。如果它们相差const、ref、指针或其他任何方式,则会得到此错误。

如果这不能解决您的问题,请发布更完整的信息。至少是在InteSiVis和testApp中实现的签名。