我正在使用最新版本的Code :: Blocks。我有一个函数传入一个字符串和一个向量。该函数编译时没有错误。但是,当我运行调试器时,它立即引导我到第118行(我已经注意到)并给我带来麻烦。出现的错误说“找不到当前功能的界限”。找不到当前函数的范围(Code :: Blocks)C++
这是一个函数,它接受一行变量声明的代码(如“var c = 0”),并获取它的变量并将其值添加到向量v中, int value
和string 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我正在阅读的文本文件。不知道这是一个普遍问题,还是其他编译器无法理解的问题。
'char d [0];'看起来不对 –
@AntonSavin使用atoi函数,您需要有一个常量字符值。我认为这就是它所指的。 –