错误:创建抽象类的实例时,不允许使用抽象类类型的对象

时间:2015-10-02 00:27:13

标签: c++ instance

我正在尝试创建.dll文件。我定义了一个类

class Contactmodel

public:
Contactmodel();
virtual ~Contactmodel();

然后,我尝试创建此类的实例

virtual Contactmodel *clone() const {return new Contactmodel();}

但是当我编译时,它会导致错误:创建抽象类的实例时不允许使用抽象类类型的对象

您能否告诉我创建实例以及如何修复它我做错了什么?谢谢!

我的头文件

#pragma once
// contactmodelCohesive.h

#include "contactmodel/src/contactmodelmechanical.h"

#ifdef Cohesive_LIB
#  define Cohesive_EXPORT EXPORT_TAG
#elif defined(NO_MODEL_IMPORT)
#  define Cohesive_EXPORT
#else
#  define Cohesive_EXPORT IMPORT_TAG
#endif

namespace cmodelsxd {
    using namespace itasca;

    class ContactModelCohesive : public ContactModelMechanical {
    public:

        Cohesive_EXPORT ContactModelCohesive();
        Cohesive_EXPORT virtual ~ContactModelCohesive();
        virtual void                copy(const ContactModel *c);
        virtual void                archive(ArchiveStream &); 

        virtual QString  getName() const { return "Cohesive"; }
        virtual void     setIndex(int i) { index_=i;}
        virtual int      getIndex() const {return index_;}

        enum PropertyKeys { 
              kwKn=1
            , kwKs                            
            , kwFric   
            , kwForce
            , kwMoment
            , kwState
            , kwRGap
            , kwConRad
            , kwSig
            , kwTau 
            , kwEmod
            , kwKRatio
            , kwTenStrength
            , kwCoh 
            , kwPhiF
            , kwPhiD
            , kwInelasDeform
            , kwDamagevar
            , kwAlpha
            , kwLamda
            , kwwsig
            , kwwcoh
        };

        virtual QString  getProperties() const { 
            return " kn"
                   ",ks"
                   ",fric"
                   ",coh_force"
                   ",coh_moment"
                   ",state"
                   ",rgap"
                   ",con_rad"
                   ",sigma"
                   ",tau"
                   ",emod"
                   ",kratio"
                   ",ten_strength"
                   ",cohesion"
                   ",phi_f"
                   ",phi_d"
                   ",in_deform"
                   ",D_var"
                   ",alpha"
                   ",lamda"
                   ",w_sig"
                   ",w_coh";
        }

        enum FishCallEvents { fActivated=0, fBondBreak };
        virtual QString  getFishCallEvents() const { return "contact_activated,contact_break"; }
        virtual QVariant getProperty(uint i,const IContact *) const;
        virtual bool     getPropertyGlobal(uint i) const;
        virtual bool     setProperty(uint i,const QVariant &v,IContact *);
        virtual bool     getPropertyReadOnly(uint i) const;


         enum MethodKeys { kwDeformability=1
                        , kwcohBond
                        , kwcohUnbond
        };

        virtual QString  getMethods() const {
            return "deformability"
                   ",bond"
                   ",unbond";
        }

        virtual QString  getMethodArguments(uint i) const; 

        virtual bool     setMethod(uint i,const QVector<QVariant> &vl,IContact *con=0); // Base 1 - returns true if timestep contributions need to be updated

        virtual uint     getMinorVersion() const;

        virtual bool     validate(ContactModelMechanicalState *state,const double &timestep);

        //virtual bool    endPropertyUpdated(const QString &name,const IContactMechanical *c);

        virtual bool     forceDisplacementLaw(ContactModelMechanicalState *state,const double &timestep);

        virtual DVect2   getEffectiveTranslationalStiffness() const { DVect2 ret = effectiveTranslationalStiffness_; return ret;}
        virtual DAVect   getEffectiveRotationalStiffness() const {if (!cohProps_) return DAVect(0.0); return cohProps_->coh_AngStiff_;}

        virtual ContactModelCohesive *clone() const { return NEWC(ContactModelCohesive()); }

        virtual double   getActivityDistance() const {return rgap_;}

        virtual bool     isOKToDelete() const { return !isBonded(); }

        virtual void     resetForcesAndMoments() { cohF(DVect(0.0)); cohM(DAVect(0.0));}

        virtual bool     checkActivity(const double &gap) { return (gap <= rgap_ || isBonded()); }

        virtual bool     isBonded() const { return cohProps_ ? (cohProps_->coh_state_==3) : false; }

        bool     hasPBond() const {return cohProps_ ? true:false;}
        const double & rgap() const {return rgap_;}
        void     rgap(const double &d) {rgap_=d;}
        int      cohState()   const {return hasPBond() ? cohProps_->coh_state_: 0;}   
        void     cohState(int i) { if(!hasPBond()) return; cohProps_->coh_state_=i;}
        double   cohKn() const {return (hasPBond() ? (cohProps_->coh_kn_) : 0.0);}
        void     cohKn(const double &d) { if(!hasPBond()) return; cohProps_->coh_kn_=d;}
        double   cohKs() const {return (hasPBond() ? (cohProps_->coh_ks_) : 0.0);}
        void     cohKs(const double &d) { if(!hasPBond()) return; cohProps_->coh_ks_=d;}
        double   cohFric() const {return (hasPBond() ? (cohProps_->coh_fric_) : 0.0);}
        void     cohFric(const double &d) { if(!hasPBond()) return; cohProps_->coh_fric_=d;}
        double   cohTen() const {return (hasPBond() ? (cohProps_->coh_ten_) : 0.0);}
        void     cohTen(const double &d) { if(!hasPBond()) return; cohProps_->coh_ten_=d;}
        double   cohLamda() const {return (hasPBond() ? (cohProps_->coh_lamda_) : 0.0);}
        void     cohLamda(const double &d) { if(!hasPBond()) return; cohProps_->coh_lamda_=d;}
        double   cohAlpha() const {return (hasPBond() ? (cohProps_->coh_alpha_) : 0.0);}
        void     cohAlpha(const double &d) { if(!hasPBond()) return; cohProps_->coh_alpha_=d;}
        double   cohInelas() const {return (hasPBond() ? (cohProps_->coh_inelas_) : 0.0);}
        void     cohInelas(const double &d) { if(!hasPBond()) return; cohProps_->coh_inelas_=d;}
        double   cohDvar() const {return (hasPBond() ? (cohProps_->coh_dvar_) : 0.0);}
        void     cohDvar(const double &d) { if(!hasPBond()) return; cohProps_->coh_dvar_=d;}
        double   cohCoh() const {return (hasPBond() ? (cohProps_->coh_coh_) : 0.0);}
        void     cohCoh(const double &d) { if(!hasPBond()) return; cohProps_->coh_coh_=d;}
        double   cohPhiD() const {return (hasPBond() ? (cohProps_->coh_phid_) : 0.0);}
        void     cohPhiD(const double &d) { if(!hasPBond()) return; cohProps_->coh_phid_=d;}
        double   cohPhiF() const {return (hasPBond() ? (cohProps_->coh_phif_) : 0.0);}
        void     cohPhiF(const double &d) { if(!hasPBond()) return; cohProps_->coh_phif_=d;}
        double   cohwsig() const {return (hasPBond() ? (cohProps_->w_sig) : 0.0);}
        void     cohwsig(const double &d) { if(!hasPBond()) return; cohProps_->w_sig=d;}
        double   cohwcoh() const {return (hasPBond() ? (cohProps_->w_coh) : 0.0);}
        void     cohwcoh(const double &d) { if(!hasPBond()) return; cohProps_->w_coh=d;}
        DVect    cohF() const {return hasPBond() ? cohProps_->coh_F_: DVect(0.0);}
        void     cohF(const DVect &f) { if(!hasPBond()) return; cohProps_->coh_F_=f;}
        DAVect   cohM() const {return hasPBond() ? cohProps_->coh_M_: DAVect(0.0);}
        void     cohM(const DAVect &m) { if(!hasPBond()) return; cohProps_->coh_M_=m;}
        DVect2   cohTransStiff() const {return hasPBond() ? cohProps_->coh_TransStiff_: DVect2(0.0);}
        void     cohTransStiff(const DVect2 &f) { if(!hasPBond()) return; cohProps_->coh_TransStiff_=f;}
        DAVect   cohAngStiff() const {return hasPBond() ? cohProps_->coh_AngStiff_: DAVect(0.0);}
        void     cohAngStiff(const DAVect &m) { if(!hasPBond()) return; cohProps_->coh_AngStiff_=m;}

        const DVect2 & effectiveTranslationalStiffness()  const          {return effectiveTranslationalStiffness_;}
        void           effectiveTranslationalStiffness(const DVect2 &v ) {effectiveTranslationalStiffness_=v;}

    private:
        static int index_;

        struct cohProps {
            cohProps() : coh_kn_(0.0), coh_ks_(0.0), coh_fric_(0.0), coh_state_(0), coh_ten_(0.0), coh_coh_(0.0), 
                        coh_alpha_(1.0), coh_lamda_(0.0), coh_phid_(0.0), coh_phif_(0.0), coh_dvar_(0.0), coh_inelas_(0.0),
                        coh_F_(DVect(0.0)), coh_M_(DAVect(0.0)), coh_TransStiff_(0.0), coh_AngStiff_(0.0) {}
            // parallel bond
            int     coh_state_;         // Contact mode - 0 (NBNF), 1 (NBFT), 2 (NBFS), 3 (B)
            double  coh_kn_;           // normal stiffness
            double  coh_ks_;           // shear stiffness
            double  kn0;
            double  ks0;
            double  coh_fric_;          // Moment contribution factor 
            double  coh_ten_;          // normal strength 
            double  coh_coh_;           // friction angle
            double  ten0;
            double  coh0;
            double  coh_inelas_;           // friction angle
            double  coh_alpha_;   // soften normal stiffness
            double  coh_lamda_;      // soften normal stiffness
            double  coh_phid_;      // soften normal stiffness
            double  phid0;
            double  coh_phif_;      // soften normal stiffness
            double  coh_dvar_;      // soften normal stiffness
            DVect   coh_F_;            // Force in parallel bond
            DAVect  coh_M_;            // moment in parallel bond
            double  mn;
            double  ms;
            double  w_sig;
            double  w_coh;
            DVect2  coh_TransStiff_;    // (Normal,Shear) Translational stiffness of the parallel bond
            DAVect  coh_AngStiff_;      // (Normal,Shear) Rotational stiffness of the parallel bond
        };

        void   updateEffectiveStiffness(ContactModelMechanicalState *state);

        DVect3 cohData(const IContactMechanical *con) const; // Bond area and inertia
        DVect2 cohSMax(const IContactMechanical *con) const; // Maximum stress (tensile,shear) at bond periphery
        double cohShearStrength(const double &cohArea) const;      // Bond shear strength

        double      rgap_;      // reference gap for the linear part

        cohProps *   cohProps_;     // The parallel bond properties

        DVect2  effectiveTranslationalStiffness_;

    }; //ContactModelCohesive
} // cmodelsxd

2 个答案:

答案 0 :(得分:1)

拥有抽象基类的原因是提供具体类必须实现的接口。您不应该创建抽象基类的实例。

使clone()成为纯虚函数,不要在基类中实现它。只在具体的类中实现它。

virtual Contactmodel *clone() const = 0;

答案 1 :(得分:0)

如果Contactmodel是抽象类(即包含纯虚函数),则无法创建Contactmodel的实例。 clone通常被定义为基类中的纯虚函数,在派生中重载,如

struct IClonable 
{
    virtual IClonable* clone() const = 0;
    virtual ~IClonable() = default;
};

class Foo: public IClonable
{
public:    
    Foo* clone() const override
    {
        return new Foo{*this};
    }
};

int main() 
{
    Foo* foo = new Foo;
    Foo* clone = foo->clone();
    delete clone;
    delete foo;
}

或者,使用智能指针

#include <memory>

struct IClonable 
{
    virtual std::unique_ptr<IClonable> clone() const = 0;
    virtual ~IClonable() = default;
};

class Foo: public IClonable
{
public:    
    std::unique_ptr<IClonable> clone() const override
    {
        return std::make_unique<Foo>(*this);
    }
};

int main() 
{
    std::unique_ptr<IClonable> up_foo{new Foo};
    std::unique_ptr<IClonable> up_clone = up_foo->clone();
}