繁体   English   中英

实例化子类的多态问题

[英]Polymorphism issue with instantiating sub classes

我有一个抽象基类Company和2个子类fProfitNfprofit ,它们在我尝试实例化两个子类时从Company派生,但出现以下错误。

C:\Qt\Qt5.3.0\Tools\QtCreator\bin\test\nfprofit.cpp:19: error: prototype for 'QString Nfprofit::getName()' does not match any in class 'Nfprofit'
 QString Nfprofit::getName() {
         ^

C:\Qt\Qt5.3.0\Tools\QtCreator\bin\test\nfprofit.h:17: error: candidate is: virtual QString Nfprofit::getName() const
     QString getName() const;
             ^

我的CompanyNfprofit标题如下。 知道我在做什么错吗?

码:

class Company {
public:
virtual QString getName() const = 0;
virtual QString getDateFormed() const = 0;
virtual QString toString() const = 0;
};

class Nfprofit : public Company
{
public:
    Nfprofit(QString name, QString date, bool charitable);

    //setters
    void setName(QString name);
    void setDateFormed(QString date);
    void setCharitableStatus(bool charitable);

    //getters
    QString getName() const;
    QString getDateFormed() const;
    QString getCharitableStatus() const;

    QString toString() const;
private:
    QString m_name, m_dateFormed;
    bool m_charitable;
};

这是错误消息告诉您的内容:

您的成员被声明为const

QString getName() const;

但是您的定义不是const

QString Nfprofit::getName() { .... }

您需要将其设置为const

QString Nfprofit::getName() const { .... }
                            ^^^^^

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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