gpt4 book ai didi

c++ - 命名空间 'std' 中的“bad_cast”未命名类型错误

转载 作者:行者123 更新时间:2023-11-28 04:45:37 24 4
gpt4 key购买 nike

<分区>

给定下一个代码:

#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;
}

我收到此消息错误,但我不明白是什么原因,我该如何解决?

'bad_cast' in namespace 'std' does not name a type

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com