我正在使用最新版本的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函數,您需要有一個常量字符值。我認爲這就是它所指的。 –