C++代码使用std::cin时报错无匹配'operator>>'问题求助
问题排查与修复方案
错误原因
编译报错的核心问题出在代码行std::cin >> CurrentAge >> std::endl;:
std::endl是仅适用于输出流(std::ostream类型,如std::cout)的流操纵符,作用是输出换行符并刷新输出缓冲区- C++标准库没有为输入流
std::istream重载支持std::endl的operator>>运算符,因此调用时会提示不存在匹配的运算符
修复方案
直接删除std::cin行末尾的>> std::endl即可,修改后完整可运行代码为:
#include <iostream> using namespace std; int main() { int Principal = 0; int RetirementAge = 65; int CurrentAge = 0; std::cout << "What is your current age?" << std::endl; std::cin >> CurrentAge; std::cout << CurrentAge << "Is this correct?" << std::endl; return 0; }
如果需要在输入后额外插入空行提升可读性,可以在std::cin语句后新增std::cout << std::endl;实现。
内容的提问来源于stack exchange,提问作者Weket Lava
相关产品推荐
相关产品推荐

