简体   繁体   English

即使使用#include,在Visual Studio中构建也无​​法识别getline()或stoi() <string> 和 <fstream> 声明

[英]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.

相关问题 Visual Studio停止识别任何包含文件 - Visual Studio stopped recognizing any include files 即使包含std名称空间后,fstream在Visual Studio 2017中也不起作用 - fstream is not working in Visual Studio 2017 even after including the std namespace 代码无法识别“ #include”语句 - Code not recognizing “#include” statements 即使关闭了预编译的标头,Visual Studio也无法识别stod() <string> 包括在内 - Visual Studio not recognizing stod() even with precompiled headers turned off and<string> included 即使指定了ProjectDir,使用包含文件也无法识别的pro * C文件进行编译时,编译错误(Visual Studio 2012) - Compilation error when compiling pro*C file with include files not recognizing, even after specifying the ProjectDir (Visual Studio 2012) 为什么在Visual Studio 2010中可以使用stoi函数 - Why stoi function is available in Visual Studio 2010 Visual Studio中的fstream指针问题 - Issues with fstream pointer in visual studio 为什么getline函数无法将其识别为字符串? - why the getline function isn't recognizing it as a string? “无法在Visual Studio 2008 Express Edition中打开包含文件:&#39;fstream.h&#39;”错误 - “Cannot open include file: 'fstream.h'” error in Visual Studio 2008 Express Edition fstream getline() 未处理的异常 - fstream getline() Unhandled exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM