简体   繁体   中英

No match for ‘operator<<’ in std

I just started learning C++, and this test seemed like a good idea so i tried doing it, doesn't seem to work, and it really doesn't make sense why (to me).

#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;

}

You forgot to include <string> , string isn't a basic C++ datatype; use #include <string> after iostream, without the spaces after the greater than and less than signs.

It does not make sense to cin something into endl . cin is a stream to get data from, but the endl is a thing to end the line, as @arsdever commented.

Simply remove it, and your code will compile:

#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;
}

Although, you may want to first read the user's input, and then print it. Now, you print the predefined by you value of the variable (and then print an end of line), and then read the input from the user for that specific variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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