Compilation error while trying to inherit from std :: runtime_error

I am trying to compile this with g ++ under Ubuntu:

#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 

I get an error message:

 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 

I had this problem for a while, and I can’t say what is wrong with it: /

+6
source share
3 answers

The compiler, through its error messages, tells you important things. If we take only the first message (it is always useful to keep track of the compilation tasks one by one, starting from the first thing that happened):

 parseexception.h:9: error: expected class-name before '{' token 

This means that you are looking at line 9. In the code before the problem "{" there is a problem: the class name is not valid. You can deduce from this that the compiler may not know what "std :: runtime_error" is. This means that the compiler does not find "std :: runtime_error" in the headers you specify. Then you need to check if the correct headers are included.

A quick search in the C ++ help documentation will tell you that std :: runtime_error is part of the <stdexcept> header, not the <exception> . This is a common mistake.

You just need to add this header and the error will disappear. Of the other error messages, the compiler tells you about the same things, but in the constructors.

Learning to read compiler error messages is a very important skill to avoid blocking compilation problems.

+14
source

include <stdexcept> .

+6
source

You need to have the full std::runtime_error available at the point you exit.

 #include <stdexcept> 
+1
source

Source: https://habr.com/ru/post/896026/


All Articles