我在我的编程实验室中收到错误"stoi is not a member of std"

I'm getting the error "stoi is not a member of std" in myprogramminglab

本文关键字:is stoi not member std of 错误 编程 我的 实验室      更新时间:2023-10-16

编辑:这个问题被标记为重复。我确实看过之前我能找到的所有类似的问题,但还没有找到答案。基本上,我无法控制程序的编译方式(尽管我认为它已经使用c++11了),所以我要么寻找stoi在这种上下文中不起作用的原因,要么寻找任何可以达到相同目的的替代语句。

我对c++很陌生,正在为课堂做这个项目。它必须通过myprogramminglab.com提交,所以我不能修改编译器。我遇到的问题是,我得到以下错误:

CTest.cpp: In function 'void getTime(int&, int&, bool&, std::string)':
CTest.cpp:38: error: 'stoi' is not a member of 'std'
CTest.cpp:39: error: 'stoi' is not a member of 'std'

我从谷歌中了解到,这通常意味着我的编译器没有配置为c++ 11。但就像我说的,我无法控制我的编程实验室的这方面。我错过了一些东西在我的代码,可能能够得到stoi启动和运行。如果没有,有没有我可以使用的"老"方法?在我的书中我找不到一个好的解决方案(尽管我承认我可能只是不知道要寻找什么),并且在我通过这个编译错误之前无法测试我的代码的其余部分。

如果从代码中看不明显,则将其赋值为以HH:MM xm格式输入,并计算两个时间之间的分钟数,并以分钟(以及小时和分钟)为单位输出该差的量。我还必须使用一个名为computeDifference的函数和上面提到的参数(尽管我添加了字符串参数,因为我想在函数之外获得输入)。

#include <iostream>
#include <string>
using namespace std;
int computeDifference(int hours_par, int minutes_par, bool isAM_par, int hoursF_par, int minutesF_par, bool isAMF_par);
void getTime(int& minutes, int& hours, bool& isAM);
int main()
{
    int hours, minutes, fut_hours, fut_minutes, difference;
    bool isAM, fut_isAM;
    cout << "Enter start time, in the format 'HH:MM xm', where 'xm' isn";
    cout << "either 'am' or 'pm' for AM or PM:";
    getTime(hours, minutes, isAM);
    cout << "Enter future time, in the format 'HH:MM xm', where 'xm' isn";
    cout << "either 'am' or 'pm' for AM or PM:";
    getTime(fut_hours, fut_minutes, fut_isAM);
    difference = computeDifference(hours, minutes, isAM, fut_hours, fut_minutes, fut_isAM);
    cout << "There are " << difference << " minutes (" << (difference - (difference%60))/60 << " hours and " << difference%60 << " minutes) between" << hours << ":" << minutes<< " and " << fut_hours << ":" << fut_minutes;
    return 0;
}
int computeDifference(int hours_par, int minutes_par, bool isAM_par, int hoursF_par, int minutesF_par, bool isAMF_par) {
    int start_total = 0, future_total = 0;
    start_total += hours_par * 60;
    start_total += minutes_par;
    if (isAM_par)
        start_total += 720;
    future_total += hoursF_par * 60;
    future_total += minutesF_par;
    if (isAMF_par)
        future_total += 720;
    return future_total - start_total;
      }
void getTime(int& minutes, int& hours, bool& isAM, string timestamp) {
    string hoursS, minutesS;
    hoursS = timestamp.substr(0, 2);
    minutesS = timestamp.substr(3, 2);
    hours = std::stoi(hoursS);
    minutes = std::stoi(minutesS);
    isAM = ("am" == timestamp.substr(6, 2));
    cout << hours << " " << minutes << " " << isAM;
    cout << timestamp;
}

我尝试了几种不同的方法,例如不使用std::部分。但这似乎给了我最小的错误…

任何帮助都将非常感激!谢谢!

std::stoi是c++11中可用的函数,所以如果您有c++11可用的编译器,请提供编译器选项-std=c++11

否则使用atoi()代替stoi(),如下所示:

   #include<cstdlib>
    hours   = std::atoi(hoursS.c_str());
    minutes = std::atoi(minutesS.c_str());

如何不控制编译的这一方面呢?如果您使用的是GCC编译器,请使用以下选项重新编译:

g++ -std=c++11 <files>

您也可以在这里使用c atoi()函数:http://www.cplusplus.com/reference/cstdlib/atoi/

同样,要测试代码,只需注释掉那些行,并为这两个变量提供虚拟值,以确保其余代码按预期工作。最后,我在代码中没有看到任何地方允许用户输入任何值。尝试允许用户输入一些值,将它们作为字符串进行验证,然后尝试将它们转换为整数并执行需要执行的操作。

既然它是一个提供服务的网站,也许你应该直接联系他们。http://www.pearsonmylabandmastering.com/northamerica/myprogramminglab/students/support/index.html