Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何调试std::bad_cast异常 class-GAGenome{ 虚空方法(){}; }; 模板 伽雷级{ }; 模板 GA1DRARAYGENOME类:公共GAArray,公共GAGenome{ }; int main(){ GA1阵列基因组; const-GAGenome&reference=基因组; 自动转换=动态转换(参考); }_C++_Debugging - Fatal编程技术网

C++ 如何调试std::bad_cast异常 class-GAGenome{ 虚空方法(){}; }; 模板 伽雷级{ }; 模板 GA1DRARAYGENOME类:公共GAArray,公共GAGenome{ }; int main(){ GA1阵列基因组; const-GAGenome&reference=基因组; 自动转换=动态转换(参考); }

C++ 如何调试std::bad_cast异常 class-GAGenome{ 虚空方法(){}; }; 模板 伽雷级{ }; 模板 GA1DRARAYGENOME类:公共GAArray,公共GAGenome{ }; int main(){ GA1阵列基因组; const-GAGenome&reference=基因组; 自动转换=动态转换(参考); },c++,debugging,C++,Debugging,这显然是错误的程序(因为模板参数不同)与 terminate called after throwing an instance of 'std::bad_cast' what(): std::bad_cast Aborted (core dumped) 除了运行时错误消息之外,有没有一种方法可以精确诊断出发生了什么错误?可以向我指出int/float错误的东西?我正在寻找一个描述性的错误消息,如 const ga1daraygenome&不能强制转换为const ga1daraygen

这显然是错误的程序(因为模板参数不同)与

terminate called after throwing an instance of 'std::bad_cast'
  what():  std::bad_cast
Aborted (core dumped)
除了运行时错误消息之外,有没有一种方法可以精确诊断出发生了什么错误?可以向我指出int/float错误的东西?我正在寻找一个描述性的错误消息,如

const ga1daraygenome&
不能强制转换为
const ga1daraygenome&

<> P>更优,因为C++类型有时会有毛发,工具可以注意到模板参数的精确差异。

< P>可以加载程序(用GDC和GLUN中的调试信息,例如代码> > G < /代码>)在GBD中加载,告诉gdb使用
catch throw
捕获异常,然后查看调用堆栈以查看异常抛出的确切位置


std::当
dynamic\u cast
在运行时失败时,将抛出坏的\u cast

您也可以放弃直接使用
dynamic\u cast
并将其包装到您自己的模板机制中:

#include <sstream>

class my_bad_cast: public std::bad_cast {
public:
    my_bad_cast(char const* s, char const* d): _source(s), _destination(d) {
#ifdef WITH_BETTER_WHAT
        try {
            std::ostringstream oss;
            oss << "Could not cast '" << _source
                << "' into '" << _destination << "'";
            _what = oss.str();
        } catch (...) {
            _what.clear();
        }
#endif
    }

    char const* source() const { return _source; }
    char const* destination() const { return _destination; }

#ifdef WITH_BETTER_WHAT
    virtual char const* what() const noexcept {
        return not _what.empty() ? _what.c_str() : std::bad_cast::what();
    }
#endif

private:
    char const* _source;
    char const* _destination;
#ifdef WITH_BETTER_WHAT
    std::string _what;
#endif
    // you can even add a stack trace
};

template <typename D, typename S>
D my_dynamic_cast(S&& s) {
    try {
        return dynamic_cast<D>(std::forward<S>(s));
    } catch(std::bad_cast const&) {
        throw my_bad_cast(typeid(S).name(), typeid(D).name());
    }
}
#包括
类my_bad_cast:public std::bad_cast{
公众:
my_bad_cast(字符常量*s,字符常量*d):\u源、\u目标(d){
#如果你有更好的选择什么
试一试{
std::ostringstream oss;

oss但这并没有告诉我出了什么问题…另一方面,我猜指定的错误类型是唯一可能出错的事情,对吗?@user7610:是的,不幸的是,它可能来自任何使用
dynamic\u cast
的地方,因此在大型程序中可能很难发现…继续从问题我可以使用<代码> STD::CUT@ MatthieuM。Type ID(.No)。()(可以)用平台C++库提供的函数和模板参数在那里进行离散化。现在,如果我可以在没有中断的情况下从GDB重新编译它,那么就可以做到这一点。
依赖于实现。因此,在gcc上,它通常是一个损坏的名称,但这不是一个可移植的假设(尽管它在工作时非常有用)这是std异常的一个不幸的副作用:通过尝试避免内存分配,它们的消息几乎是无用的,而且由于它们没有堆栈跟踪,几乎不可能知道它们来自后验。另一方面,它们是轻量级的。。。