getline:标识符未找到。

18

我使用getline()遇到问题。

我尝试了许多示例并阅读了其他解决方案,但这没有解决我的问题。我仍然看到信息:'getline: identifier not found'

我已经包含了<stdio.h><tchar.h><iostream><conio.h><stdlib.h><fstream>,但仍然无法解决问题。

#include "stdafx.h"

using namespace std;

int main(int argc, _TCHAR* argv[])
{
    string line;
    getline(cin, line);
    cout << "You entered: " << line << endl;
}

我现在需要做什么?

我使用的是 Windows 7 64 位和 Visual Studio 2013。


5
“<string>” 是你可以找到 std::getline 函数的文件名。 - WhozCraig
http://en.cppreference.com/w/cpp/string/basic_string/getline - juanchopanza
1
瞎猜不是编程的好方法。为什么不直接在文档中查找呢? - Lightness Races in Orbit
4个回答

38
请习惯于仅阅读所使用的语言功能的文档。
cppreference 明确指出 std::getline 可能会在 string 中找到。
#include <string>

6
这应该可以解决这个问题:
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int main(int argc, _TCHAR* argv[]){
   string line;
   getline(cin, line);
   cout << "You entered: " << line << endl;
}

5
#include <string>

顺便提一下,“Murach's C++ Programming”教材错误地将getline()函数归为iostream头文件中的内容(第71页),这可能会导致一些混淆。


4

7
不是"string",而是<string>,我也遇到了这个问题,使用了cppreference - Mushy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接