过去几天我一直在努力学习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;
}`
当我尝试在VS 2015中构建此代码时,控制台无法识别getline或stoi,就好像我没有为string和fstream添加#include语句一样。我的代码有问题还是与VS有关?
答案 0 :(得分:4)
这与VS有关。
因为你有
#include "stdafx.h"
我猜你已经打开了预编译的标题,并且&#34; stdafx.h&#34;是预编译的头文件。 (这是VS中的默认名称)
如果打开了预编译标头,则会忽略预编译标头的include语句之前的任何内容。
确保#include "stdafx.h"
是文件中的第一件事(评论除外),或关闭预编译的标题。