" 'X' not declared in this scope "错误

" 'X' not declared in this scope " error

本文关键字:scope 错误 this not declared in      更新时间:2023-10-16

我对编程很陌生,我刚刚上大学一年级的第二年,所以请在技术方面放轻松。我们被要求制作一个程序,从文件中读取 10 个整数以构成一个列表,并要求用户输入一个整数"N"。如果列表中有"N",则程序应显示"已找到"和"未找到"。我在主要参数中收到一个错误,它说函数调用中的"V"、"N"和"F"未在此范围内声明"。

#include<iostream>
#include<fstream>
using namespace std;
int fRead();
int iRead();
bool search(int, int);
void display(bool);
int main() {
 fRead();
 iRead();
 search(V, N);
 display(F);
 return 0;
}
int fRead() {
 int V[10], c;
 ifstream fin;
 fin.open("lab02.in");
 for(c=0; c<10; c++)
  fin >> V[10];
 fin.close();
 return V[10];
}
int iRead() {
 int N;
 cout << "Input an integer: ";
 cin >> N;
 return N;
}
bool search(int V[10], int N) {
 bool F = false;
 if(V[10] == N)
  F = true;
 return F;
}
void display(bool F) {
 if(F == true)
  cout << "nFOUND" << endl;
 else
  cout << "nNOT FOUND" << endl;
}

局部变量(在函数内声明的变量)仅对声明它们的块(由 {} 分隔的那些东西)可见。如果要对各种操作使用不同的函数,则需要将变量作为参数传递给相应的函数。

顺便说一句,在使用结果之前,您应该始终验证读取操作是否成功,例如:

int N(-1);
if (!(std::cin >> N)) {
    std::cout << "ERROR: failed to read integern";
}

整数 V N 和 F 基本上在其他函数中描述。要解决此问题,您应该在 main 中声明它们程序应如下所示

      void f_read(v[]);
        void i_read(int &);
        bool bool(int,int);
        void disp(bool);   
         void main()
            {
                int v[10],n;
                bool f;
                f_read(v);
                i_read(n);
                f=bool(v,n);
                disp(f);
            }
    void fRead() 
{
    int c;
     ifstream fin;
     fin.open("lab02.in");
     for(c=0; c<10; c++)
      fin >> V[c];
     fin.close();
    }
    void iRead(int &n)
 {
     cout << "Input an integer: ";
     cin >> N;
    }
    bool search(int V[10], int N)
    {
     bool F = false;
int i;
for(i=0;i<=9;i++)
     if(V[i] == N)
     { 
      F = true;
     return F;
}
    }
    void display(bool F) 
    {
     if(F == true)
      cout << "nFOUND" << endl;
     else
      cout << "nNOT FOUND" << endl;
    }

所以基本上你需要传递 n 作为引用变量,默认情况下数组作为引用传递。

而且您正在寻找 v[10]=f ,这会给您另一个数组,我也纠正了