c – 尝试从std :: runtime_error继承时出现编译错误

我正在尝试用Ubuntu下的g编译它:
#ifndef PARSEEXCEPTION_H
#define PARSEEXCEPTION_H

#include<exception>
#include<string>
#include<iostream>

struct ParseException : public std::runtime_error
{
    explicit ParseException(const std::string& msg):std::runtime_error(msg){};
    explicit ParseException(const std::string& token,const std::string& found):std::runtime_error("missing '"+token+"',instead found: '"+found+"'"){};

};

#endif

我收到错误消息:

In file included from parseexception.cpp:1:
parseexception.h:9: error: expected class-name before ‘{’ token
parseexception.h: In constructor ‘ParseException::ParseException(const std::string&)’:
parseexception.h:10: error: expected class-name before ‘(’ token
parseexception.h:10: error: expected ‘{’ before ‘(’ token
parseexception.h: In constructor ‘ParseException::ParseException(const std::string&,const std::string&)’:
parseexception.h:11: error: expected class-name before ‘(’ token
parseexception.h:11: error: expected ‘{’ before ‘(’ token
enter code here

我现在已经有这个问题了,我真的不知道它有什么问题:/

解决方法

编译器通过其错误消息告诉您重要的事情.如果我们只接受第一条消息(逐个处理编译问题总是一件好事,从发生的第一个开始):
parseexception.h:9: error: expected class-name before ‘{’ token

它告诉你看第9行.“{”之前的代码中存在一个问题:类名无效.您可以从中推断出编译器可能不知道“std :: runtime_error”是什么.这意味着编译器在您提供的标头中找不到“std :: runtime_error”.然后,您必须检查是否包含正确的标题.

在C参考文档中快速搜索将告诉您std :: runtime_error是< stdexcept>的一部分.标题,而不是< exception>.这是一个常见的错误.

然后你必须添加这个标题,错误消失了.从其他错误消息中,编译器会告诉您几乎相同的内容,但是在构造函数中.

学习阅读编译器的错误消息是一项非常重要的技能,以避免在编译问题上被阻止.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...