簡體   English   中英

C ++中不允許使用抽象類型為“ NumericQuestion”的對象

[英]object of abstract type “NumericQuestion” is not allowed in c++

另外,這段代碼到底在做什么?

const int MAX_QUESTIONS = 100;
Question* questions[MAX_QUESTIONS];
questions[numQuestions++] = createQuestion("How many degrees are there in a full circle?", 360);

Question* createQuestion(const char question[], const double answer)
{
    NumericQuestion* nq = new NumericQuestion(question, answer);
        return nq;
}

非常感謝。 我真的需要你的幫助才能向我解釋

編輯:這是問題的聲明

class Question
{
    char *q;
protected:
    void writeQuestion(ostream& ostr) const;
public:
    Question(const char question[])
    {
        int n = strlen(question);
        q =  new char[n];
        strcpy(q, question);
        cout << q;
        }
    void askQuestion() const;
    virtual void getAnswer();
    virtual bool isCorrect() const=0;
};

編輯:聲明為NumericQuestion

class NumericQuestion : public Question
{
    double ans, geta;
public:
    NumericQuestion(const char question[], const double answer): Question(question)
    {
        ans = answer;
        getAnswer();
    }

    void getAnswer()
    {
        cout << "Answer: ";
        cin >> geta;
        isCorrect();
        }
    bool isCorrect()
    {
        return ((geta==ans)? true : false);
    }
};

Stackoverflow不允許我發布太多代碼

需要聲明isCorrect()constNumericQuestion

bool isCorrect() const  // <--- add const keyword here

原因是因為const是方法簽名的一部分,例如其名稱和參數。 isCorrect() const比命名的功能的不同方法isCorrect()不是常量。

當您看到帶有=0的虛擬方法時,表示該方法是“純虛擬”的。 基類通常不指定實現,但是在實例化任何派生類之前,必須定義實現。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM