beëindigen aangeroepen na het gooien van een instantie van ‘std::invalid_argument’ what(): stoi

stoi-functie crasht programma met de foutmelding

"****@****:~> 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

en ik kan niet achterhalen waarom, er wordt een instantie doorgegeven aan de functie, maar er wordt nog steeds een fout geretourneerd. Kan iemand me misschien helpen met een oplossing?

#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);
        }
    };
}

Antwoord 1, autoriteit 100%

Het betekent dat je std::stoi()slechte invoer geeft, dus het gooit een std::invalid_argumentuitzondering die u niet opvangt.

std::stoi, std::stol, std::stoll:

Uitzonderingen

  • std::invalid_argumentals geen conversie kon worden uitgevoerd

  • std::out_of_rangeals de geconverteerde waarde buiten het bereik van het resultaattype zou vallen of als de onderliggende functie (std::strtolof std::strtoll) stelt errnoin op ERANGE.

std::terminate

std::terminate() wordt aangeroepen door de C++-runtime wanneer het afhandelen van uitzonderingen om een ​​van de volgende redenen mislukt:

1) een uitzondering wordt gegenereerd en niet opgevangen(het wordt door de implementatie bepaald of er in dit geval een stapelafwikkeling wordt uitgevoerd)

U moet uw invoertekenreekswaarden die u probeert om te zetten naar gehele getallen, nogmaals controleren. Ze vertegenwoordigen geen gehele getallen.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

19 − two =

Other episodes