- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
g++ -std=c++0x m1.cpp stimulation.h stims.h Task.h exoskeleton.h A-6ren">
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
我不明白为什么,有一个实例被传递到函数中,但仍然返回错误。有人可以帮我解决问题吗?
#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);
}
};
}
最佳答案
这意味着你正在给 std::stoi()
错误的输入,所以它会抛出一个 std::invalid_argument
你没有捕捉到的异常。
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::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::invalid_argument' what() : stoi 实例后终止调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41070101/
I was wondering which way is the most recommended one in modern C++ to convert a string to a numb
我有: -Windows 7/32 位上的 cygwin 1.7.25 -g++ --版本 --> g++ (GCC) 4.8.2 -libstdc++.a --> gcc-g++-4.8.2-1 尝
我已经在 my posts 之一中询问过关于 boost::lexical_cast 的替代方案的问题,在许多回复中,我有一个建议 stoi 作为可行的替代方案。 我决定对其进行测试,令我惊讶的是,此
我正在使用最新版本的 C++,我正在尝试运行以下代码。但是,它一直告诉我 stoi “未在此范围内声明”。我是 C++ 的新手,所以如果您有任何想法,请分享。 #include using name
这个问题在这里已经有了答案: Function stoi not declared (13 个答案) 关闭 8 年前。 我正在尝试使用 stoi() 将 string 转换为 int 但我收到的错误
我正在通过编写来自用户的简单输入/输出来练习我的 C++。我现在已经搜索了一个多小时如何将字符串转换为 int。每个答案都是不同的,并且需要其他东西。 我正在使用 Code::Blocks v.13.
这个问题在这里已经有了答案: cygwin g++ std::stoi "error: ‘stoi’ is not a member of ‘std (4 个答案) 关闭 6 年前。 所以这个错误已
我有这个代码,它给我错误 terminating with uncaught exception of type std::out_of_range: stoi: out of range 我已经确定
我正在尝试将字符串解析为整数,但我不确定自己做错了什么: string input; cin >> input; int s = std::stoi(input); 这不会构建并引发错误:“stoi”
我正在使用代码块,但我无法使 stoi() 函数正常工作。我阅读了有关此问题的其他问题,但无法解决。我检查了 C+11,我正在使用命名空间 std 并且我有字符串 header 。我不知道如何解决这个
我是 Java 新手,我正在寻找 Java 中 C++ 中的类似 stoi() 的函数。 我想要这样的: 如果有像“123ABC”这样的字符串,我想提取整数中的“123”并获取“A”的索引。 我一直在
正如您所知,std::sto* 函数系列的工作方式是读取字符串中的数字,直到找到非数字,如下所示: std::string pseudoNum = "123haha"; int num = std::
我尝试编写一个将数字字符串转换为整数的函数。使用g++ 9.2.0在VS代码上运行代码时,输出错误,但是在repl.it上运行时,输出正确。这是我的代码: #include #include
我对传递到 std::stoi 的整数字符串的限制感到困惑。一个整数是 32 位的,我没记错吗?在带符号的 32 位整数中,范围可以从 −2,147,483,648 到 2,147,483,647。我
我只是通过 HackerRanks 练习我的 C++,我正在做这个问题: Problem Statement You are given an integer N. Find the digits i
我有以下测试程序: #include int main(int argc, char* argv[]) { try { return std::stoi("300012723
我上周为一项作业创建了一个复数类,我们的下一个作业是将此类修改为模板类。部分赋值重载了提取运算符,最初只处理 int 时,我将整个输入行作为一个字符串,并为实部和虚部制作子字符串,然后我使用 stoi
大多数日常用例都已转换为标准库中可用的函数。但是,我想知道其中一些是如何在幕后工作的。比如 std::stoi 背后的实际代码是什么? 最佳答案 最直接的方法是调用适当的 C 函数,在本例中为 str
c++的stoi函数定义为: int stoi(const std::string& str, std::size_t* pos = 0, int base = 10); 如您所见,base 参数默认
我正在尝试使用 stoi将字符串转换为整数,但它说它没有声明。我有标准库和 包括在内,但它仍然显示 [Error] 'stoi' was not declared in this scope 代码如下
我是一名优秀的程序员,十分优秀!