简体   繁体   English

C ++ Hello World Tutorial错误

[英]C++ Hello World Tutorial Error

I'm learning C++ by following examples in a book and after typing this out and double checking I keep getting error messages. 我正在通过以下书中的示例学习C ++,在输入后再进行双重检查后,我不断收到错误消息。 I cant figure out what is wrong. 我无法弄清楚出了什么问题。 I'm using Visual C++ 2010 if it matters. 如果重要的话,我正在使用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;
}

There are four errors. 有四个错误。 The error codes are: 错误代码是:

Error 1 error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion) i:\\helloworld.cpp 11 错误1错误C2678:二进制'>>':找不到哪个运算符带有'std :: istream'类型的左操作数(或者没有可接受的转换)i:\\ helloworld.cpp 11

Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) i:\\helloworld.cpp 15 错误2错误C2679:二进制'<<':找不到运算符,它接受类型'std :: string'的右手操作数(或者没有可接受的转换)i:\\ helloworld.cpp 15

Error 3 error C3861: 'getline': identifier not found i:\\helloworld.cpp 25 错误3错误C3861:'getline':找不到标识符i:\\ helloworld.cpp 25

Error 4 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) i:\\helloworld.cpp 29 错误4错误C2679:二进制'<<':找不到带有'std :: string'类型右手操作数的运算符(或者没有可接受的转换)i:\\ helloworld.cpp 29

缺少#include <string>表示string

As Wesley already said, ensure you have the library included. 正如韦斯利所说,确保你有图书馆。

Also, be very careful when using getline() and cin in the same program. 另外,在同一程序中使用getline()cin时要非常小心。 One can affect the other and you can get unexpected results. 一个可以影响另一个,你可以得到意想不到的结果。 To be safe, I've found that it is useful to use cin.ignore() after using a cin and before using a getline. 为了安全起见,我发现在使用cin之后和使用getline 之前使用cin.ignore()很有用。 Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM