> = int-6ren"> > = int-在 C++ 中运行代码时,我收到错误 no operator "="matches these operands operand types are std::basic_ostream> = int-6ren">
gpt4 book ai didi

c++ - 没有运算符 "="匹配这些操作数操作数类型是 std::basic_ostream> = int

转载 作者:行者123 更新时间:2023-11-28 01:37:31 31 4
gpt4 key购买 nike

在 C++ 中运行代码时,我收到错误 no operator "="matches these operands operand types are std::basic_ostream> = int 并且我不确定究竟是什么导致了错误。

#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
using namespace std;

int main()
{
int num1, num2;
double average;

// Input 2 integers
cout << "Enter two integers separated by one or more spaces: ";
cin >> num1, num2;

//Find and display their average
cout << average = (num1 + num2) / 2;

cout << "\nThe average of these 2 numbers is " << average << "endl";

return 0;
}

最佳答案

编译器处理

cout << average = (num1 + num2) / 2;

作为:

(cout << average) = ((num1 + num2) / 2);

参见 C++ operator precedence了解更多详情。

修复:

cout << (average = (num1 + num2) / 2);

更喜欢简单的语句:

average = (num1 + num2) / 2;
cout << average;

还有

cin >> num1, num2;

应该是

cin >> num1 >> num2;

关于c++ - 没有运算符 "="匹配这些操作数操作数类型是 std::basic_ostream<char, std::char_traits<char>> = int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48646447/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com