我正在使用最新版本的Code :: Blocks。我有一个传递字符串和向量的函数。该函数编译时没有错误。但是,当我运行调试器时,它立即将我引向第118行(我已经注意到)并给我带来麻烦。出现的错误说“无法找到当前函数的边界”。
这是函数,它接受一行变量声明的代码(如“var c = 0”),并获取它的变量并将其值添加到向量v,带有{的结构{1}}和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
}
免责声明:是的,这是一项家庭作业。不,我不是在寻找任何人为我完成这项任务。但是,我仍然主要是编码的新手,需要一些帮助,所以我问你是否知道发生了什么,请帮助我解决这个问题。谢谢!
编辑:我已经让这个工作在我正在读取的文本文件的另一个编译器上。不确定这是一个普遍的问题,还是其他编译器没有接受的问题。
答案 0 :(得分: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
字符串为空。
评论中还注明了其他未定义行为的实例,我不会重复这些实例。
答案 1 :(得分:0)
我发现了问题。要正确使用atoi,您不能使用字符串或字符数组中的特定字符。如果您声明char a[3]
,并且想要使用atoi,则必须使用int value = atoi(a)
而不是value = atoi(a[2])
。如果不这样做,将导致运行时错误。