首页 > 解决方案 > dynamic_cast 从右值到引用类型

问题描述

我在 hpp 文件中有这个函数宏:

#define THROW_SIMPLE(a) throw CSimpleException(__FILE__, __LINE__, dynamic_cast<const stringstream& > (stringstream() << a))

使用 c++14 编译整个包时,出现此错误:

INFO (ERR) cdalignproc_base.cpp:104:3: error: dynamic_cast from rvalue to reference type 'const std::__1::stringstream &' (aka 'const basic_stringstream<char> &')
INFO (ERR)                 THROW_SIMPLE("Cannot sort reading frames - query sequence length unknown");

有没有办法修改这个函数以继续正常运行并正确编译?
任何帮助深表感谢。谢谢

有关更多上下文,这里是对该宏的调用:

./common/basealgo.cpp       THROW_SIMPLE("Input stream error: Cannot read"); 
./common/cdalignproc_base.cpp       THROW_SIMPLE("Cannot sort reading frames - query sequence length unknown");  
./common/basedata.cpp   //  THROW_SIMPLE("Invalid Pdb ID string " << acxn);             
                    THROW_SIMPLE("Motif string parse error -- Motif = " << m_strMotif << ", error position: " << errPos);
                    THROW_SIMPLE("Invalid protein length " << aaLen << ": shorter than aligned range " << alignedLen)

编译命令如下所示:

x86_64-apple-darwin13.4.0-clang++  -stdlib=libc++ -std=gnu++14 -c  -Wall -Wno-format-y2k -m64 -fpascal-strings  -fopenmp -march=core2 -mtune=haswell -mssse3 -ftree-vectorize -fPIC -fPIE -fstack-protector-strong -O2 -pipe -stdlib=libc++ -fvisibility-inlines-hidden -std=c++14 -fmessage-length=0 -isystem $PREFIX/include -fdebug-prefix-map=$SRC_DIR=/usr/local/src/conda/blast-2.10.1 -fdebug-prefix-map=$PREFIX=/usr/local/src/conda-prefix -O2 -Wno-deprecated-register -fno-common   -D__DB_OFFLINE__ -I. -DNDEBUG -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -m64 -D_FORTIFY_SOURCE=2 -mmacosx-version-min=10.9 -isystem $PREFIX/include -I$PREFIX/include -D_MT -D_REENTRANT -D_THREAD_SAFE -I$SRC_DIR/blast/c++/ReleaseMT/inc -I$SRC_DIR/blast/c++/include  -DNCBI_APP_BUILT_AS=rpsbproc -DNCBI_BUILD_SESSION_ID=5E6DA050-CCF1-4170-BF1A-EC777D1B7288 $SRC_DIR/blast/c++/src/app/RpsbProc/common/offl_sparcle_data.cpp -o common/offl_sparcle_data.o

我努力了

#define THROW_SIMPLE(a) throw CSimpleException(__FILE__, __LINE__, dynamic_cast<stringstream& > (stringstream() << a))

但得到错误:

01:35:34 BIOCONDA INFO (ERR) /Users/distiller/project/miniconda/conda-bld/blast_1600674036922/work/blast/c++/src/app/RpsbProc/common/cdalignproc_base.cpp:104:3: error: dynamic_cast from rvalue to reference type 'std::__1::stringstream &' (aka 'basic_stringstream<char> &')
01:35:34 BIOCONDA INFO (ERR)                 THROW_SIMPLE("Cannot sort reading frames - query sequence length unknown");
01:35:34 BIOCONDA INFO (ERR)                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
01:35:34 BIOCONDA INFO (ERR) /Users/distiller/project/miniconda/conda-bld/blast_1600674036922/work/blast/c++/src/app/RpsbProc/common/normbase.hpp:145:68: note: expanded from macro 'THROW_SIMPLE'
01:35:34 BIOCONDA INFO (ERR) #define THROW_SIMPLE(a) throw CSimpleException(__FILE__, __LINE__, dynamic_cast<stringstream& > (stringstream() << a))

我努力了

#define THROW_SIMPLE(a) throw CSimpleException(__FILE__, __LINE__, stringstream(a))

但得到错误:

02:07:13 BIOCONDA INFO (ERR) /Users/distiller/project/miniconda/conda-bld/blast_1600848608737/work/blast/c++/src/app/RpsbProc/common/basedata.cpp:466:56: error: invalid operands to binary expression ('const char [37]' and 'const std::string' (aka 'const basic_string<char, char_traits<char>, allocator<char> >'))
02:07:13 BIOCONDA INFO (ERR)                         THROW_SIMPLE("Motif string parse error -- Motif = " << m_strMotif << ", error position: " << errPos);
02:07:13 BIOCONDA INFO (ERR)                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^  ~~~~~~~~~~
02:07:13 BIOCONDA INFO (ERR) /Users/distiller/project/miniconda/conda-bld/blast_1600848608737/work/blast/c++/src/app/RpsbProc/common/normbase.hpp:145:81: note: expanded from macro 'THROW_SIMPLE'
02:07:13 BIOCONDA INFO (ERR) #define THROW_SIMPLE(a) throw CSimpleException(__FILE__, __LINE__, stringstream(a))

标签: c++

解决方案


推荐阅读