[英]Polymorphism issue with instantiating sub classes
我有一個抽象基類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;
};
這是錯誤消息告訴您的內容:
您的成員被聲明為const
:
QString getName() const;
但是您的定義不是const
:
QString Nfprofit::getName() { .... }
您需要將其設置為const
:
QString Nfprofit::getName() const { .... }
^^^^^
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.