std 中的‘operator<<’不匹配

No match for ‘operator<<’ in std

我刚开始学习 C++,这个测试似乎是个好主意,所以我尝试这样做,但似乎行不通,而且(对我来说)这真的没有意义。

#include <iostream>

using namespace std;

int myNum = 5;               // Integer (whole number without decimals)
double myFloatNum = 5.32543;    // Floating point number (with decimals)
char myLetter = 'H';         // Character
string myText = "test text: test";     // String (text)
bool myBoolean = true;            // Boolean (true or false)

int main() {

    cout << myNum << endl;
    cin >> myNum >> endl;

    cout << myFloatNum << endl;
    cin >> myFloatNum >> endl;

    cout << myLetter << endl;
    cin >> myLetter >> endl;

    cout << myText << endl;
    cin >> myText >> endl;

    cout << myBoolean << endl;
    cin >> myBoolean >> endl;

    return 0;

}

你忘了include <string>,字符串不是基本的 C++ 数据类型;在iostream之后使用#include <string>,大于号和小于号后面没有空格。

将某些东西 cin 变成 endl 是有意义的。 cin 是一个从中获取数据的流,但是 endl 是结束该行的东西,正如@arsdever 评论的那样。

只需删除它,您的代码就会编译:

#include <iostream>
#include <string>    // You forgot to include that header, for using std::string
using namespace std;

int myNum = 5;
double myFloatNum = 5.32543;
char myLetter = 'H';
string myText = "test text: test";
bool myBoolean = true;

int main() {

    cout << myNum << endl;
    cin >> myNum;

    cout << myFloatNum << endl;
    cin >> myFloatNum;

    cout << myLetter << endl;
    cin >> myLetter;

    cout << myText << endl;
    cin >> myText;

    cout << myBoolean << endl;
    cin >> myBoolean;

    return 0;
}

尽管如此,您可能希望先读取 用户的输入,然后然后 打印它。现在,您打印由您预定义的变量值(然后打印行尾),然后读取用户对该特定变量的输入。