You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

error C2679错误求助:二进制'<<'无匹配重载运算符问题

Fixing Error C2679: Binary '<<' No Operator for Overloaded Function

Let’s cut straight to the root of your error—you’re trying to use std::endl with an input stream (std::cin), which is invalid and causing the compiler to throw that C2679 error.

The Problem in Your Code

Look at these lines in your main function:

std::cin >> myArray[0] >> std::endl;
std::cin >> myArray[1] >> std::endl;

std::endl is a manipulator exclusively for output streams (like std::cout). It inserts a newline and flushes the output buffer—operations that have no place in input operations. When you chain it with std::cin using >>, the compiler can’t find a valid operator that accepts std::endl (an overloaded function) as a right-hand operand for input, hence the error.

The Permanent Fix

Remove the >> std::endl from your input lines entirely. Here’s the corrected code:

#include <iostream>
void printArrayValues(int x, int y) {
    std::cout << x << std::endl;
    std::cout << y << std::endl;
}
int main() {
    int myArray[2];
    std::cout << "Please enter what you want the first element in 'myArray' to be: ";
    std::cin >> myArray[0]; // No std::endl here
    std::cout << "Please enter what you want the second element in 'myArray' to be: ";
    std::cin >> myArray[1]; // No std::endl here
    printArrayValues(myArray[0], myArray[1]);
    return 0;
}

Why Adding <string> Seemed to Work (But Wasn’t the Real Solution)

Including <string> might have temporarily hidden the error in some cases because it pulls in extra standard library code that coincidentally includes some operator overloads. But this was just a coincidence—your core issue (misusing std::endl with input) wasn’t fixed, so the error came back.

Key Takeaways

  • Input streams don’t use std::endl: std::endl is for output only. For input, just use >> to read values directly.
  • Newlines are handled automatically: When reading integers with std::cin, the newline character from the user pressing Enter is skipped automatically when reading the next value, so you don’t need any extra code to handle it here.

That’s all you need to do to resolve the error for good!

内容的提问来源于stack exchange,提问作者UnknownProgrammer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:16:09