【发布时间】:2014-02-06 18:08:53
【问题描述】:
我通过遵循书中的示例来学习 C++,在输入并仔细检查后,我不断收到错误消息。我不知道出了什么问题。如果重要的话,我正在使用 Visual C++ 2010。
#include <iostream>
using namespace std;
int main()
{
// Prompt the user for data
cout << "Please enter two words:" << endl;
//Read in the values
string b, c;
cin >> b >> c;
// Give feedback
cout << "I understood: "
<< b << ", and "
<< c << endl;
// NOw, lets's read a whole line of text as a single entity
cout << "Now, type in a whole line of text, "
<< "with as many blanks as you want:"
<< endl;
//getline() is a function; we'll talk more about them in Part3
string wholeLine;
getline( cin, wholeLine );
//In the cout statement below, remember that \"
// is an escape sequence!
cout << "I understood: \"" << wholeLine << "\"" << endl;
// And we're done!
return 0;
}
有四个错误。 错误代码是:
错误 1 错误 C2678: 二进制 '>>' : 未找到采用 'std::istream' 类型左侧操作数的运算符(或没有可接受的转换) i:\ helloworld.cpp 11
错误 2 错误 C2679: 二进制 '
错误 3 错误 C3861: 'getline': identifier not found i:\helloworld.cpp 25
错误 4 错误 C2679: 二进制 '
【问题讨论】:
-
你是否加入了
stdafx.h? -
我最好的猜测是你必须包含 stdio.h 或 stdafx.h 或其他东西。显然,iostream 中没有包含预期的内容。
-
#include <string>绝对是必需的。没有它,您就没有正确的getline()声明。 -
不只是
getline(),如果不包含string标头,您也不能使用string类。 -
所以应该...使用命名空间std;正在使用命名空间 stdafx.h
标签: c++