抽象类型 "NumericQuestion" 的对象在 C++ 中是不允许的
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);
}
};
堆栈溢出不允许我发布这么多代码
您需要在NumericQuestion
:中将isCorrect()
声明为const
bool isCorrect() const // <--- add const keyword here
原因是const
是方法签名的一部分,就像它的名称和参数一样。isCorrect() const
是一个不同于名为isCorrect()
的非常量函数的方法。
当您看到一个带有=0
的虚拟方法时,这意味着该方法是"纯虚拟的"。基类通常不会指定实现,但在实例化任何派生类之前,必须定义一个实现。
相关文章:
- 为什么 Clang 不允许"and"作为函数名称?
- 不允许在向量中添加更多元素
- std::带有自定义缓冲区的 iostream 不允许我写入
- Visual Studio 2017 不允许我创建 C++ 专用模板
- 返回时不允许隐式转换
- 为什么 c++(g++) 不允许模板返回类型和函数名称之间有空格?
- 为什么 c++ 不允许(自动)强制转换?
- 为什么 c++11 不允许使用自动
- 为什么不允许成员函数和非成员函数之间的函数重载?
- 为什么不允许使用可变长度数组作为向量元素?
- 余数除法和不允许除以零 (c++) 时遇到问题
- C++从外部类继承的嵌套类;不允许使用不完整的类型
- 在 c++ 中三元运算符中不允许继续(关键字)吗?
- 为什么在指向对象的迭代器上调用函数不允许我更改对象本身?
- 错误:在第 6 行'{'标记之前,此处不允许使用函数定义
- 不允许在类定义之外重写
- 不允许运算符 const 参数调用 const 成员函数
- E0322:不允许使用抽象类类型 " " 的对象
- error dllimport 函数的定义不允许在一个特定的联合中,而其他类、结构和联合将按预期导出
- C++ 我的开关格式中的循环不允许我显示菜单选项或接受输入?