簡體   English   中英

“未在此范圍內聲明'X'”錯誤

[英]“ 'X' not declared in this scope ” error

我對編程非常陌生,我剛上大學一年級的第二學期,所以在技術方面請放輕松一點。 我們被要求制作一個程序,該程序從文件中讀取10個整數以組成一個列表,並要求用戶輸入整數“ N”。 如果列表中為“ N”,則程序應顯示“ FOUND”和“ NOT FOUND”。 我在參數方面遇到了一個主要錯誤,它說函數調用中的“ 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 integer\n";
}

整數VN和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,這會給您另一個數組,我也糾正了這一點

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM