Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting this error: invalid operands of types ‘int’ and ‘<unresolved overloaded function type>’ to binary ‘operator<<’

Tags:

c++

I am the definition of a beginner. I am working with C++ in my school's Linux server. I have been working at this program for a few hours and I can't figure out what I'm doing wrong. I've reassigned variables and restated my formulas but nothing works. Please help.

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

const int f=5;

int main ()
{
        int a,b,c,d,e,sum,avg;
        cout << "Please enter five numbers. " << endl;
        cin >> a >> b >> c >> d >> e;
        sum= a+b+c+d+e;
        cout << "The average of those numbers is: " << endl;
        cout << avg =(sum / f) << endl ;
return 0;
}

The error states: invalid operands of types ‘int’ and ‘’ to binary ‘operator<<’

like image 707
Bigrednerd82 Avatar asked Jul 03 '15 03:07

Bigrednerd82


1 Answers

Basically the problem is how cout << avg =(sum / f) << endl is parsed.

<< is left associative and has higher precedence than = so the expression is parsed as

(cout << avg) = ((sum/f) << endl)

Now the right hand side of your assignment is int << endl which raises the error, since the operation makes no sense (<< it's not defined for int, decltype(endl) arguments)

like image 181
Jack Avatar answered Oct 01 '22 12:10

Jack