【问题标题】:I'm getting the error "stoi is not a member of std" in myprogramminglab [duplicate]我在 myprogramminglab 中收到错误“stoi 不是 std 的成员”[重复]
【发布时间】:2015-09-24 01:23:08
【问题描述】:

编辑:此问题已被标记为重复。我确实浏览了所有以前我能找到但没有找到答案的类似问题。基本上,我无法控制程序的编译方式(尽管我认为它已经在使用 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 进行配置。但就像我说的那样,我无法控制 myprogramminglab 的这方面。我是否在我的代码中遗漏了一些可能能够启动并运行的东西。或者,如果没有,是否有我可以使用的“旧”方法?我在我的书中找不到一个好的解决方案(尽管我承认我可能只是不知道要寻找什么)并且在我克服这个编译错误之前无法测试我的其余代码。

如果从代码中不明显,则分配它以 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' is\n";
    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' is\n";
    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:: 部分。但这似乎给了我最少个错误......

任何帮助将不胜感激!谢谢!

【问题讨论】:

  • 您使用的是 C++11 吗?这些是 C++11 特性。如果您使用的是 g++,则可以使用 -std=c++11 编译器选项打开 C++11 功能。
  • Rhino,这就是我最初使用它的方式,没有 std:: 前缀,但它也不起作用。
  • 这是一个奇怪的错误。在不相关的说明中,您的 getTime() 函数原型确实与它的函数定义匹配。
  • @RSahu 我不确定。该程序需要通过 pearson 教育网站提交,我没有关于编译器的信息。我知道它是作为我教 c++11 的书的伴侣。所以我认为它应该用 c++11 编译,但我无法改变它的工作方式。
  • @LesleyGushurst 感谢您的关注!

标签: c++ string int std type-conversion


【解决方案1】:

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());

【讨论】:

  • 这成功了!尝试使用 atoi 时我错过了 .c_str() 。谢谢!
  • 很高兴知道它对您有所帮助!
  • 提供 -std=c++11 不会解决问题,因为它也不会解决问题 -std=c++14。
【解决方案2】:

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

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

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

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

【讨论】:

  • 谢谢。很不幸,但就像我在线程上回答的那样,该程序需要通过一个名为 myprogramminglab 的网站提交,它是我的书的一个配套网站,可以测试你的代码。我无权访问编译器(我什至看不到它正在使用什么编译器!)但我假设因为它与我的书一起使用,并且这本书教授 c++11,它意味着以这种方式编译。
  • 我现在正在尝试 atoi。我输入了它为 atoi 列出的 include 指令,我更改的代码的唯一部分是: hours = atoi(hoursS); 52 分钟 = atoi(分钟S);但现在出现此错误 CTest.cpp: In function 'void getTime(int&, int&, bool&)': CTest.cpp:51: error: cannot convert 'std::string' to 'const char*' for argument '1' to 'int atoi(const char*)' CTest.cpp:52: error: cannot convert 'std::string' to 'const char*' for argument '1' to 'int atoi(const char*)'
  • @Confucius说看我的回答,你需要使用c_str()函数将字符串转换为c字符串
  • 该错误的原因是 atoi() 函数需要一个 const char* ,它不是您声明的 std::string 类型。您需要将 std::string 转换为 const char*。为此,请使用 std::string 的成员函数 c_str()。 cplusplus.com/reference/string/string/c_str 另外,修复 getTime() 函数的函数签名以匹配定义。
  • 你说得对,我在滥用 atoi 并忽略了 c_str() 解决了它。感谢您的帮助!
【解决方案3】:

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

【讨论】:

  • 欢迎来到 Stack Overflow!虽然您的答案是在上下文中,但请记住它应该解决问题中的错误。这里是a few tips
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-20
  • 1970-01-01
  • 2014-02-23
  • 2021-08-26
  • 2014-04-06
  • 2014-11-23
  • 2020-02-02
相关资源
最近更新 更多