runtime_error was not declared in this scope for g

2020-07-06 03:01发布

问题:

The same code is working fine on gcc 4.5.2 but when trying to compile it on gcc 4.1.2, I get the error ‘runtime_error’ was not declared in this scope.

I do have

#include <stdexcept>

Is this a problem with gcc 4.1.2?

Code excerpt

// Constructor
if (resource cannot be acquired)
  throw std::runtime_error("Blah Blah");

回答1:

Visual Studio says that runtime_error should be defined in <stdexcept>, so I'm guessing that GCC 4.1.2 is just out of date here.



回答2:

Do you have using namespace std; or using std::runtime_error;? If not, then you need to fully qualify the name and use std::runtime_error rather than just runtime_error.



回答3:

gcc 4.1 is relatively old. 4.5 is more standard compliant. Maybe you triggered a compiler's bug



标签: c++ linux gcc