的实例抛出后调用终止
(c++) terminate called after throwing an instance of
我在程序'抛出'std::out_of_range "实例后调用终止'中遇到这个错误,下面是我的代码:
grade.h
#ifndef GRADE_H_
#define GRADE_H_
#include <string>
#include <iostream>
using namespace std;
class Grade
{
int mid_term, final;
double total;
public:
Grade *next;
Grade();
Grade(int i_mid_term, int i_final);
void readFile(string _file);
void printList();
void subString(string s);
void Show();
void addTail(Grade *q);
};
#endif
grade.cpp
#include "grade.h"
#include <stdlib.h>
#include <fstream>
Grade *tail;
Grade *head;
Grade::Grade(int i_mid_term, int i_final)
{
mid_term = i_mid_term;
final = i_final;
// total = i_total;
}
Grade::Grade()
{
head = tail = NULL;
}
void Grade::addTail(Grade *q)
{
if (tail != NULL){
tail -> next = q;
}else{
head = tail = q;
}
tail = q;
q -> next = NULL;
}
void Grade::readFile(string _file)
{
ifstream fin;
fin.open(_file.c_str());
if(!fin.is_open())
{
cout<<"Can't read.n";
exit(1);
}
else
{
string s = "";
while (getline(fin, s))
{
subString(s);
}
fin.close();
}
}
void Grade::printList()
{
Grade *q;
cout<<"The grade list is:n";
q = head;
int i = 1;
while (q != NULL)
{
cout<<"nGrade no: "<<i<<endl;
q-> Show();
q = q->next;
i++;
}
}
void Grade::subString(string s)
{
int mid_term;
int final;
string temp;
// Mid
int mark = s.find(":");
mid_term = atoi(s.substr(0,mark).c_str());
// cout << mid_term << endl;
// Final
temp = temp.substr(mark+1);
mark = temp.find(":");
final = atoi(temp.substr(0, mark).c_str());
// cout << final << endl;
Grade *q = new Grade(mid_term, final);
this -> addTail(q);
}
void Grade::Show()
{
cout << mid_term << "-" << final << endl;
}
编译器没有显示任何错误或警告,这使得很难找到错误。这是我第一次遇到这个问题。请帮我一下。谢谢。
@YePhIcK是正确的。这是一个运行时错误,而不是编译错误。可能的罪魁祸首是你在Grade::subString
中的一个substr
调用。我的钱在
temp = temp.substr(mark+1);
如果没有找到第一个冒号,将会导致问题。
查找错误的一种便宜的方法是将代码封装在try/catch (std::exception)中,并不断缩小范围,直到找到错误行。不过,调试器可能是更好的解决方案。
相关文章:
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 我收到以下错误:抛出'std::bad_alloc'实例后终止调用
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- 在抛出 'std::runtime_error' 的实例后终止调用 what(): Filebuf 和 ostream 的 I/O 错误
- 在字符串函数中抛出'char const*'实例后调用的终止
- 调用 raise(signal) 会终止 c++ 程序
- 在抛出 what() 的实例后调用'std::logic_error'终止:basic_string::_M_construct 空无效
- 抛出实例'std::out_of_range'后调用的终止
- 程序终止时调用函数
- 在抛出 'std::regex_error' 的实例后调用终止 what(): 括号未关闭
- 在抛出 'std::invalid_argument' 的实例后调用终止 what(): stoi 错误 in leet
- CPP-Netlib 问题,在抛出 'boost::exception_detail::clone_impl 的实例后调用终止
- 如何使用管理权限调用终止进程
- 在抛出 'std::bad_alloc' 的实例后调用终止 what(): std::bad_alloc 中止
- 在抛出 'std::bad _alloc' 的实例后调用终止 what(): std::bad_alloc 在 c++ 中
- 的实例抛出后调用终止
- 在没有活动异常的情况下调用c++终止
- 内存管理 - C++错误:抛出"std::bad_alloc"实例后调用终止
- 代码调用终止而不是引发异常