【发布时间】: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