简体   繁体   中英

Polymorphism issue with instantiating sub classes

I have an abstract base class Company and 2 sub classes fProfit and Nfprofit that derive from Company when I try to instantiate either of the sub classes I get the following error.

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

My headers for Company and Nfprofit are below. Any idea what I'm doing wrong?

code:

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

This is what the error message is telling you:

Your member is a declared as const :

QString getName() const;

But your definition isn't const :

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

You need to make it const :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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