gpt4 book ai didi

c++ - 错误 : object of abstract class type is not allowed when create an instance of a abstract class

转载 作者:行者123 更新时间:2023-11-28 06:03:08 25 4
gpt4 key购买 nike

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

class Contactmodel

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

然后,我尝试创建这个类的一个实例

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

但是编译的时候报错:object of abstract class type is not allowed when create an instance of a abstract class

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

我的头文件

#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

最佳答案

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

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

virtual Contactmodel *clone() const = 0;

关于c++ - 错误 : object of abstract class type is not allowed when create an instance of a abstract class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32898797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com