C++ 在抛出 'std::invalid_argument' what() 实例后调用终止:stoi
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41070101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
terminate called after throwing an instance of 'std::invalid_argument' what(): stoi
提问by dennis
stoi function crashes program with the error message
stoi 函数使程序崩溃并显示错误消息
"****@****:~> g++ -std=c++0x m1.cpp stimulation.h stims.h Task.h exoskeleton.h ARAIG_Sensors.h Profile.h
ARAIG_Sensors.h:1:9: warning: #pragma once in main file [enabled by default]
Profile.h:1:9: warning: #pragma once in main file [enabled by default]
*****@****:~> a.out StimulationConfig.csv TaskConfiguration.csv SampleProfileConfig.csv SampleOutput.txt
First : a.out
Second : StimulationConfig.csv
terminate called after throwing an instance of 'std::invalid_argument'
what(): stoi
Aborted
and I can't figure out why, there is a instance being passed into the function but still returning an error. Can someone please help me out with a solution maybe?
我不知道为什么,有一个实例被传递到函数中,但仍然返回错误。有人可以帮我解决问题吗?
#pragma once
#include <string>
#include <vector>
#include "Task.h"
#include <ostream>
#include "ARAIG_Sensors.h"
namespace m1
{
class Profile
{
ARAIG_Sensors araig;
std::vector<Task> ToRun;
std::vector<Task> Completed;
std::string studentFN;
std::string studentLN;
std::string studentSN;
std::string flightFN;
std::string flightLN;
std::string flightEN;
std::ostream& os;
struct calibration {
int Max;
int Min;
};
calibration cal;
public:
Profile(std::string fn, std::ostream& o, ARAIG_Sensors& a) : os(o)
{
araig = a;
//parsing student
std::ifstream infile(fn);
std::string line;
std::getline(infile, line);
int f = line.find_first_of(",");
studentFN = line.substr(0, f);
line = line.substr(f + 1);
f = line.find_first_of(",");
studentLN = line.substr(0, f);
studentSN = line.substr(f + 1);
//parsing flight
std::getline(infile, line);
f = line.find_first_of(",");
flightFN = line.substr(0, f);
line = line.substr(f + 1);
f = line.find_first_of(",");
flightLN = line.substr(0, f);
flightEN = line.substr(f + 1);
//parsing calibration
std::getline(infile, line);
f = line.find_first_of(",");
cal.Min = stoi(line.substr(0, f));
std::cout << cal.Min << std::endl;
line = line.substr(f + 1);
cal.Max = stoi(line);
std::list<Task> temp = araig.gettasks();
while (std::getline(infile, line))
{
for (std::list<Task>::iterator i = temp.begin(); i != temp.end(); i++)
{
if ((*i).getName() == line)
{
ToRun.push_back(*i);
break;
}
}
}
}
void displayToRun()
{
for (std::vector<Task>::iterator i = ToRun.begin(); i != ToRun.end(); i++)
{
(*i).execute(os);
}
}
void displayCompleted()
{
for (std::vector<Task>::iterator i = Completed.begin(); i != Completed.end(); i++)
{
(*i).execute(os);
}
}
void displayNext()
{
std::vector<Task>::iterator i = ToRun.begin();
(*i).execute(os);
}
void displayLast()
{
std::vector<Task>::iterator i = Completed.end();
(*i).execute(os);
}
void Run()
{
//excute next Task and move Task to Completed
std::vector<Task>::iterator i = ToRun.begin();
Task t = *i;
t.execute(os);
ToRun.erase(i);
Completed.push_back(t);
}
};
}
回答by Remy Lebeau
It means you are giving std::stoi()
bad input, so it is throwing a std::invalid_argument
exception that you are not catching.
这意味着你提供了std::stoi()
错误的输入,所以它抛出了一个std::invalid_argument
你没有捕捉到的异常。
std::stoi, std::stol, std::stoll:
std::stoi、std::stol、std::stoll:
Exceptions
std::invalid_argument
if no conversion could be performed
std::out_of_range
if the converted value would fall out of the range of the result type or if the underlying function (std::strtol
orstd::strtoll
) setserrno
toERANGE
.
例外
std::invalid_argument
如果无法进行转换
std::out_of_range
如果转换后的值超出结果类型的范围,或者基础函数(std::strtol
或std::strtoll
)设置errno
为ERANGE
.
std::terminate() is called by the C++ runtime when exception handling fails for any of the following reasons:
1) an exception is thrown and not caught(it is implementation-defined whether any stack unwinding is done in this case)
当异常处理由于以下任何原因而失败时,C++ 运行时会调用 std::terminate():
1)一个异常被抛出但未被捕获(在这种情况下是否进行任何堆栈展开是实现定义的)
You need to double-check your input string values that you are trying to convert to integers. They do not represent integers.
您需要仔细检查您尝试转换为整数的输入字符串值。它们不代表整数。