2016-03-02 158 views
0

我正在使用最新版本的Code :: Blocks。我有一個函數傳入一個字符串和一個向量。該函數編譯時沒有錯誤。但是,當我運行調試器時,它立即引導我到第118行(我已經注意到)並給我帶來麻煩。出現的錯誤說「找不到當前功能的界限」。找不到當前函數的範圍(Code :: Blocks)C++

這是一個函數,它接受一行變量聲明的代碼(如「var c = 0」),並獲取它的變量並將其值添加到向量v中, int valuestring name

char get_variable_declaration(string line, vector<variable> &v) 
{ 
    string b; 
    variable t; 
    char d[0]; 
    int counter = 0; 
    int a; 
    for (int i = 0; i<line.size(); i++) { 
     if (line[i] == 'r' && counter != 1) { 
      b[0] = line [i+2]; 
      counter ++; 
     } 
     if (line[i] == '=') { 
      b[1]=line[i+1]; 
     } 
    } 
    t.name = b[0]; 
    d[0] = b[1]; 
    a = atoi (d); 
    t.value = a; 
    v.push_back (t); 
    return b[0]; 

    //This function will take in a line of code 
    //that is confirmed to have a variable declaration 
    //it will add the variable to the list of 
    //vectors 
} 

下面是當它被稱爲:

bool read_code(string file_name, vector<funct> &my_functions, vector<variable> & v) 
{ 
    vector<string> code; 
    string s; 
    std::size_t found; 
    bool flag; 
    funct new_function; 

    ifstream in; 
    in.open(file_name.c_str()); 
    if(in.is_open()) 
    { 
     //read in file line by line and put it into a vector called code 
     while(in.peek()!=EOF) 
     { 
      getline(in,s); 
      code.push_back(s); 
     } 
     in.clear(); 
     in.close(); 

     //read through each line of the code, determine if it's a variable or function (definition or call) 
     //here it makes reference to functions (listed following this one) which will actually decompose the line 
     //for information 
     for(int i=0;i<code.size();i++) 
     { 
      //check if it's a variable declaration 
      found = code[i].find("var"); 
      if(found!=std::string::npos) //its a variable declaration 
       get_variable_declaration(code[i], v); //ERROR CANNOT FIND.. 

      //check if it's a function. it'll go in the list of functions 
      found = code[i].find("funct"); 
      if (found!=std::string::npos) //that means it's a function 
      { 
       new_function.funct_name=get_function_name(code[i]); 
       new_function.commands.clear(); 
       i+=2; //skip over the open curly brace 
       flag=false; 
       while(!flag) 
       { 
        found = code[i].find("}"); 
        if(found==std::string::npos) 
        { 
         new_function.commands.push_back(code[i]); 
         i++; 
        } 
        else 
        { 
         my_functions.push_back(new_function); 
         flag=true; 
        } 
       } 
      } 
     } 

     return true; 
    } 
    else 
    { 
     cout << "Cannot locate this file" << endl; 
     return false; 
    } 
} 

免責聲明:是的,這是一個家庭作業。不,我不想找人爲我完成這項任務。但是,我仍然是編碼方面的新手,需要一些幫助,所以我問你是否知道發生了什麼,請幫我解決這個問題。謝謝!

編輯:我已經得到這個工作在另一個編譯器W/O我正在閱讀的文本文件。不知道這是一個普遍問題,還是其他編譯器無法理解的問題。

+0

'char d [0];'看起來不對 –

+0

@AntonSavin使用atoi函數,您需要有一個常量字符值。我認爲這就是它所指的。 –

回答

0

我發現了這個問題。要正確使用atoi,您不能使用字符串或字符數組中的特定字符。如果您聲明char a[3],並且您想要使用atoi,則必須使用它,如int value = atoi(a)而不是value = atoi(a[2])。如果你不這樣做,它會導致運行時錯誤。

0

的多個問題的這部分代碼:

string b; 

for (int i = 0; i<line.size(); i++) { 
    if (line[i] == 'r' && counter != 1) { 
     b[0] = line [i+2]; 
     counter ++; 
    } 
    if (line[i] == '=') { 
     b[1]=line[i+1]; 
    } 
} 

問題:

  • 如果line的最後一個字符是 'R',可能會出現未定義的行爲。

  • 如果line中的倒數第二個字符爲'r',則可能發生未定義的行爲。

  • 如果line中的最後一個字符爲'=',則會發生未定義的行爲。

  • b[0]b[1]這兩個分配都是未定義的行爲。 b字符串爲空。

還有的已經在評論,我不會重複注意到不確定的行爲等情況。

+0

d [0]的聲明有問題嗎?我如何使用atoi()函數而不聲明它? –

+0

d [0]聲明一個大小爲零的數組,它包含零個元素。然後你嘗試初始化這個數組,它有零個元素。你能理解這個嗎? –

+0

啊,是的,它的確如此。謝謝。 –