简体   繁体   中英

Invalid operands of types ‘int’ and ‘<unresolved overloaded function type>’ to binary ‘operator<<’

I was trying to sum two arrays into a third one!!

int main()
{
   int RoadHeights[2000] , TopoHeights[2000] , Differences[4000] , i , n ;

   cout << "Enter the number of stations! " << endl;
   cin >> n;

   cout << "Enter the heights of stations on Road! " << endl;

   for ( i=0 ; i<n ; i++ )
      cin >> RoadHeights[i];

   cout << "Enter the heights of stations on Ground! " << endl;

   for ( i=0 ; i<n ; i++ )
      cin >> TopoHeights[i];

   cout << "Height differences are: " << endl;

   for ( i=0 ; i<n ; i++ )
      cout << Differences [4000] = RoadHeights[i] - TopoHeights[i] << endl;

   return 0;
}

2 things: Differences[4000] is out of bounds, and the = operator has lower precedence than the << operator, so wrap up your expression with parentheses:

cout << (Differences [i] = RoadHeights[i] - TopoHeights[i]) << endl;

Otherwise, cout << Differences[i] is evaluated first, returning an ostream& , effectively becoming

cout << Differences[i]; 
cout = (RoadHeights[i] - TopoHeights[i]) << endl;

Clearly the second line is an error

This will solve your compiler problem, but I'm guessing you have more logic problems in there. For example, Hardcoded array sizes, but user input for the size afterwards? What if n is 5000 ? Try using a std::vector instead.

std::vector<int> RoadHeights, TopoHeights, Differences;
int i , n ;

cout << "Enter the number of stations! " << endl;
cin >> n;
RoadHeights.resize(n);
TopoHeights.resize(n);
Differences.resize(n);
// proceed as normal

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM