C++ 分配抽象类类型错误的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24424996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:44:05  来源:igfitidea点击:

allocating an object of abstract class type error

c++abstract-class

提问by user3737372

Hi I'm getting the following error and am really unsure why.

嗨,我收到以下错误,我真的不确定为什么。

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

This occurs when I make class inresivis inherit from the ofxMidiListener class and I get the following error in the main source file

当我使类 inresivis 从 ofxMidiListener 类继承时会发生这种情况,并且在主源文件中出现以下错误

int main( ){

int main(){

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

}

}

This is really confusing as I have tried this with another example in the exact way and do not get this error.

这真的很令人困惑,因为我已经以确切的方式在另一个示例中尝试了这一点,并且没有收到此错误。

class testApp : public ofBaseApp, public ofxMidiListener {

int main(){
    ofSetupOpenGL(640, 480, OF_WINDOW);
    ofRunApp(new testApp());
}

Could you give me an idea as to why I'm getting this error I'm calling the class in the exact same way. Thanks in advance.

你能告诉我为什么我会收到这个错误吗?我以完全相同的方式调用类。提前致谢。

///----------------------------------Edit InteSiVis.h

///---------------------------------编辑 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);

};

//----------------VIRTUAL FUNCTION vorticitydistribution.h

//----------------虚拟函数 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
        {

} ;

回答by

This is how things works:

这就是事情的运作方式:

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
}; 

When you declare testApp like this class testApp : public Base { ... };:

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

normal non-virtual methodsare inherited as they are declared inside Base, and are immutable.

normal virtual methodsare inherited as they are declared inside Base, you can use them >as they already are declared, or redefine them to fit a particular purpose.

pure virtual methodsare not defined, the parent class only say "if you inherit from me you HAVE TO implement those by yourself, strictly matching my prototype (How I defined them)

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

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

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

If you don't follow those rules, you'll get errors.

如果不遵守这些规则,就会出错。



Now, you have to be sure that there is no pure virtual methodes inside ofBaseAppneither ofxMidiListenerthat are not implemented into the children class.

现在,你必须要确保有内部没有纯虚methodes ofBaseApp既不ofxMidiListener未落实到儿童类。

Since you state that class testAppdoesnt do any error, you must have a pure virtual methods in InteSiVisfrom the parents classes that you forgot to implement.

由于您声明类testApp没有执行任何错误,因此您必须在InteSiVis 中有一个来自您忘记实现的父类的纯虚拟方法。

回答by Spacemoose

Well, you haven't posted enough information to be sure.

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

Typically when you get a message that the compiler is unable to create a class of abstract type where you are trying to instantiate a class which inherits from some interface, it means that you have not provided an implementation of one of the purely virtual methods specified by the interface.

通常,当您收到编译器无法创建抽象类型类的消息时,您尝试实例化从某个接口继承的类时,这意味着您尚未提供由指定的纯虚拟方法之一的实现界面。

In your implementation of testApp, have you specified an overide of any methods which you have not specified in InteSiVis? The signatures must match exactly. If they differ by a const, by a ref, a pointer, or any other way, you will get this error.

在您的 testApp 实现中,您是否指定了您未在 InteSiVis 中指定的任何方法的覆盖?签名必须完全匹配。如果它们因常量、引用、指针或任何其他方式不同,则会出现此错误。

If this does not solve your problem, please post more complete information. At least the signatures of what have implemented in InteSiVis and testApp.

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