Error: no match for 'operator>>' for std::cin

Error: no match for 'operator>>' for std::cin

我正在为一个练习编码:

Write a complete program that reads an integer from the user,doubles it using the doubleNumber() function,and then prints the doubled value out to the console.

#include <iostream>

int doubleNumber(int x)
{
    return 2*x;
}

int main()
{

    int a;
    std::cout << "Enter an integer :" ;
    std::cin >> a >> std::endl;
    std::cout << doubleNumber(a) << endl;
    return 0;
}

编译时出现的错误是:

error: no match for 'operator >>'

有什么想法吗?

你的最终 endl 应该是 std::endl;

也把cin后面的endl去掉,cin只取值,不需要加换行,cout就这样

std::endl 实际做的是,将换行符写入流 ('\n'),然后使用 std::flush 刷新流,将缓冲区中的所有内容写入屏幕。

请注意,它写入换行符,这意味着它不适合输入,因此不应该用于此类。

此外,您忘记在输出的第二行 endl 之前指定 std::

你有两个问题。您缺少 endl 的标准。这是识别范围所必需的,因为 endl 来自 std。

您的另一个问题是当您使用 cin 时,无需添加 std::endl。 std::endl 只代表行尾(创建一个新行)。 cin 命令仅接受输入,std::endl 不是您可以输入的变量。

如果您修复这些问题,您的程序将 运行 正常。

固定代码:

#include <iostream>

int doubleNumber(int x)
{
    return 2*x;
}

int main()
{

    int a;
    std::cout << "Enter an integer :" ;
    std::cin >> a;
    std::cout << doubleNumber(a) << std::endl;
    return 0;
}