[英]Why is this std::out_of_range being thrown in this function?
下面是發生此錯誤的函數:
std::string DataTranslation::getMeshName(std::string meshLink)
{
File input(this->datafilename);
std::cout << "the line count of " << this->datafilename << " = " << input.lineCount() << ".\n";
std::cout << "code above is working properly if this prints.\n";
return "_";
}
運行:
the line count of ./res/data/resourcelist.data = 6.
code above is working properly if this prints.
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
就是這樣。 以前,該功能要復雜得多,在調試過程中,我將其全部注釋掉了,剩下的就是我上面寫的內容。 由於return "_";
似乎引發了異常return "_";
。
我是在犯基本錯誤,還是真的很奇怪?
terminate called after throwing an instance of 'std::out_of_range'
terminate
被調用通常是由於“逃避”析構函數的異常。
what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
當您對std::vector
的元素進行檢查訪問(認為.at()
)時,通常會拋出該異常。
因此,請在File
析構函數內查找錯誤的選中矢量訪問。 更好的是,要調試此類問題,請在調試器中運行程序,甚至可以為拋出的異常添加一個斷點(或在std::terminate
之上); 請注意,使用gdb
甚至不需SIGABRT
std::terminate
結果會生成SIGABRT
,它會自動進入調試器。
(gdb) r
Starting program: /home/matteo/scratch/a.out
terminate called after throwing an instance of 'int'
Program received signal SIGABRT, Aborted.
0x00007ffff76c1418 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
54 ../sysdeps/unix/sysv/linux/raise.c: File o directory non esistente.
(gdb) bt
#0 0x00007ffff76c1418 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
#1 0x00007ffff76c301a in __GI_abort () at abort.c:89
#2 0x00007ffff7ae484d in __gnu_cxx::__verbose_terminate_handler() ()
from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ffff7ae26b6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ffff7ae2701 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffff7ae2919 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x000000000040077e in A::~A() ()
#7 0x0000000000400729 in foo() ()
#8 0x0000000000400749 in main ()
(gdb)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.