我有一个抽象基类Company
和2个子类fProfit
和Nfprofit
,当我尝试实例化其中任何一个子类时,它从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;
^
Company
和Nfprofit
的标题如下。知道我做错了吗?
代码:
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;
};
答案 0 :(得分:1)
这是错误消息告诉您的内容:
您的会员声明为const
:
QString getName() const;
但您的定义不是const
:
QString Nfprofit::getName() { .... }
您需要将其设为const
:
QString Nfprofit::getName() const { .... }
^^^^^