未在范围内声明.多个错误

Not declared in scope. Multiple errors

本文关键字:错误 声明 范围内      更新时间:2023-10-16

由于某种原因,我不断收到范围错误。我继续收到多个范围错误以及。

#include<iostream.h>
#include<fstream.h>
#include<assert.h>
 void ScanInFile(int ScanInValues[], int *total)
{
   int i=0;
   while(!cin>>&ScanInValues[i].eof()){
      i++;
      *total = i;
   }
}
void SortFile(int DataSetValues[], int TotalValues)
{
   int i, j, temp;
   for(i = 0; i < (TotalValues - 1); i++){
      for(j=0; j < TotalValues - i -1; j++){
      if(DataSetValues[j] > DataSetValues[j+1]){
         temp = DataSetValues[j];
         DataSetValues[j] = DataSetValues[j+1];
         DataSetValues[j+1] = temp;
         }
      }
   }
}
int main(void)
{
   int i, AmountOfValues=0;
   int values[100]={ 0 };
 ScanInFile(values, &AmountOfValues);
 SortFile(values, AmountOfValues);
  for(i=0; i < AmountOfValues; i++){
  cout<<values[i];
   }
   cout<<endl;
   return 0;
}

由于某种原因,G++ 不会编译程序。我继续收到一个错误,说 endl 和 cout,而 eof 不在范围内。我哪里做错了?

这些对象在命名空间内声明std。您可以在它们前面加上std::std::cout << ...),或者在 cpp 文件的开头添加using namespace std;

修复 eof 错误后,您还需要检查数组的越界访问,因为您在写入之前从不检查大小。

除了 Eric 所说的之外,你的代码中还有更多问题需要指出。在您的 ScanInFile 函数中有一个您编写了这一行:

while (!cin >> &ScanInValues[i].eof())

此行将编译,但它将执行与预期非常不同的操作。我假设您正在此行执行提取,但希望在未到达文件末尾时执行这些提取。不需要.eof(),因为流将通过隐式转换为布尔值来分析流状态本身。这是按以下方式完成的:

while (cin >> ScanInValues[i])

我不想过度编译我的解释,但我只想强调这是执行提取的首选方法。使用!eof()作为提取的条件几乎总是错误的方式。