[英]Building in Visual Studio not recognizing getline() or stoi() even with #include <string> and <fstream> statements
I've been trying to learn C++ over the past couple of days, and ran into a problem when I was trying to use the getline() and stoi() methods in some practice code: 在过去的几天里,我一直在尝试学习C ++,但是在尝试在一些实践代码中使用getline()和stoi()方法时遇到了一个问题:
#include <string>
#include <fstream>
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
string numberGuessed;
int intNumberGuessed = 0;
do {
cout << "Guess a number between 1 and 10";
getline(cin, numberGuessed);
intNumberGuessed = (stoi(numberGuessed));
cout << intNumberGuessed << "\n";
} while (intNumberGuessed != 4);
cout << "You win\n";
return 0;
}`
When I tried to build this code in VS 2015, the console could not identify getline or stoi as if I hadn't added #include statements for string and fstream. 当我尝试在VS 2015中构建此代码时,控制台无法识别getline或stoi,就好像我没有为string和fstream添加#include语句一样。 Is there something wrong with my code or is it something to do with VS?
我的代码有问题吗?还是与VS有关?
It's something to do with VS. 这与VS有关。
Since you have 既然你有
#include "stdafx.h"
I'm guessing you have precompiled headers turned on, and "stdafx.h" is the precompiled header. 我猜您已打开了预编译头,并且“ stdafx.h”是预编译头。 (That's the default name in VS)
(这是VS中的默认名称)
With precompiled headers turned on, anything before the include statement for the precompiled header is ignored . 启用预编译头文件后, 将忽略包含在该预编译头文件中的所有语句。
Either make sure #include "stdafx.h"
is the very first thing in the file (except for comments), or turn off precompiled headers. 确保
#include "stdafx.h"
是文件中的第一件事(注释除外),或者关闭预编译的头文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.