2016-03-02 165 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

啊,是的,它的确如此。谢谢。 –