given the next code:
#include <iostream>
using std::cout;
using std::endl;
using std::cerr;
class A {
public:
virtual ~A() {
}
};
class B: public A {
public:
};
int main() {
int n = 4;
A a;
A& base = a;
B* ptr = dynamic_cast<B*>(&base);
if (ptr == NULL) {
cerr << "base is not a B";
}
try {
B& derived = dynamic_cast<B&>(base);
derived = *ptr;
} catch (std::bad_cast&) { // ERROR
cerr << "base is not a B";
}
if (n == 3) {
}
return 0;
}
I get this message error and I don't understand what is the reason and how can I fix it?
'bad_cast' in namespace 'std' does not name a type
If you look up the documentation at http://en.cppreference.com/w/cpp/types/bad_cast it tells you at the top which include is required for each class/function. In this case you need to include <typeinfo>