类型'int'和'<unresolved overloaded =“”function =“”type =“”>'to binary'运算符&lt;&lt;'的操作数无效

时间:2015-05-16 23:53:32

标签: c++ arrays

我试图将两个数组合并为第三个数组!!

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;
}

1 个答案:

答案 0 :(得分:2)

2件事:差异[4000]超出范围,=运算符的lower precedence比&lt;&lt;运算符,所以用括号括起你的表达式:

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

否则,首先评估cout << Differences[i],返回ostream&,实际上变为

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

显然第二行是错误

这将解决您的编译器问题,但我猜你在那里有更多的逻辑问题。例如,硬编码数组大小,但之后用户输入的大小?如果n5000,该怎么办?请尝试使用std::vector

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