C++ 函数 stoi 未声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22084783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:55:45  来源:igfitidea点击:

Function stoi not declared

c++stringpointersint

提问by user3258512

I'm trying to use stoito convert a string to an integer, however it says it's not declared. I have the standard library and the <string>included, but it still says [Error] 'stoi' was not declared in this scope

我正在尝试stoi将字符串转换为整数,但是它说它没有声明。我有标准库和<string>包含的库,但它仍然说[Error] 'stoi' was not declared in this scope

The code is the following:

代码如下:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>

using namespace std;

int main()
{
string end, init;
cout << "Introduction" << endl;
cout << "Start time (xx:yy)" << endl;
cin >> init;
string hours0 = init.substr(0,2);
int hours = stoi(hours0);
cout << hours << endl;
system("pause");
return 0;

}

Either tell me why it isn't working, or give me a second option to do it, please.

请告诉我为什么它不起作用,或者请给我第二个选择。

回答by Captain Obvlious

std::stoiwas introduced in C++11. Make sure your compiler settings are correct and/or your compiler supports C++11.

std::stoi是在 C++11 中引入的。确保您的编译器设置正确和/或您的编译器支持 C++11。

回答by Eamonn Kenny

The answers above are correct, but not well explained.

上面的答案是正确的,但没有很好地解释。

g++ -std=c++11 my_cpp_code.cpp

Add -std=c++11 to your compiler options since you are most likely using an older version of debian or ubuntu which is not using by default the new c++11 standard of g++/gcc. I had the same problem on Debian Wheezy.

将 -std=c++11 添加到您的编译器选项中,因为您很可能使用的是旧版本的 debian 或 ubuntu,它们默认不使用 g++/gcc 的新 c++11 标准。我在 Debian Wheezy 上遇到了同样的问题。

http://en.cppreference.com/w/cpp/string/basic_string/stol

http://en.cppreference.com/w/cpp/string/basic_string/stol

shows in really small writing to the right in green that c++11 is required.

以非常小的绿色文字在右侧显示需要 c++11。

回答by nneonneo

stoiis a C++11 function. If you aren't using a compiler that understands C++11, this simply won't compile.

stoi是一个 C++11 函数。如果您使用的不是理解 C++11 的编译器,这将无法编译。

You can use a stringstreaminstead to read the input:

您可以使用 astringstream来读取输入:

stringstream ss(hours0);
ss >> hours;

回答by Daniel

stoi is available "since C++11". Make sure your compiler is up to date.

stoi 可用“自 C++11”。确保您的编译器是最新的。

You can try atoi(hours0.c_str()) instead.

你可以试试 atoi(hours0.c_str()) 代替。

回答by pistol_pete

instead of this line -

而不是这条线 -

int hours = stoi(hours0);

int hours = stoi(hours0);

write this -

写这个——

int hours = atoi(hours0.c_str());

int hours = atoi(hours0.c_str());

Reference : int atoi(const char *str)

参考: int atoi(const char *str)

回答by M.M

In comments under another answer, you indicated you are using a dodgy version of g++under MS Windows.

在另一个答案下的评论中,您表示您使用的是g++MS Windows 下的狡猾版本。

In this case, -std=c++11as suggested by the top answer would still not fix the problem.

在这种情况下,-std=c++11正如最佳答案所建议的那样仍然无法解决问题。

Please see the following thread which does discuss your situation: std::stoi doesn't exist in g++ 4.6.1 on MinGW

请参阅以下讨论您的情况的线程:std::stoi 在 MinGW 上的 g++ 4.6.1 中不存在

回答by Doug

#include <algorithm>

Include this and then you can compile it using...

包括它,然后您可以使用...

g++ -Wall -std=c++11 test.cpp -o test

g++ -Wall -std=c++11 test.cpp -o test

You can also add "cd /d %~dp0" as the first line of a .bat file in the same directory as your source file so all you have to do is double click on the .bat file for an "automated" compilation.

您还可以将“cd /d %~dp0”添加为源文件所在目录中 .bat 文件的第一行,这样您只需双击 .bat 文件即可进行“自动”编译。

Hope this helps!

希望这可以帮助!

回答by Riya Arora

Install the latest version of TDM-GCC here is the link-http://wiki.codeblocks.org/index.php/MinGW_installation

在这里安装最新版本的 TDM-GCC 是链接 - http://wiki.codeblocks.org/index.php/MinGW_installation

回答by Sisnett

Are you running C++ 11? stoi was added in C++ 11, if you're running on an older version use atoi()

你在运行 C++ 11 吗?stoi 是在 C++ 11 中添加的,如果您在旧版本上运行,请使用 atoi()

回答by Nassima Noufail

Add this option: -std=c++11while compiling your code

添加此选项:-std=c++11在编译代码时

g++ -std=c++11 my_cpp_code.cpp