微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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>.这是一个常见的错误.

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

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

原文地址:https://www.jb51.cc/c/116901.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐