I am trying a debugging tool for the first time and I came up to the following problem:
我第一次尝试调试工具,我遇到了以下问题:
When I go step by step through the code with a debugger, and when I step into function ComputeInterest()
and put watch on i
(mark i
and right click on it), watch informs me that
当我一步一步地使用调试器进行代码时,当我进入函数ComputeInterest()并将watch放在I(标记I并右键单击它)时,watch会告诉我这一点。
'i' is not available in current context
“i”在当前环境中是不可用的。
I'm also curious why watch didn't already put i
into local variables. This is my code:
我也很好奇为什么手表没有把我放入局部变量中。这是我的代码:
#include <iostream>
double computeInterest (double base_val, double rate, int years)
{
double final_multiplier;
for ( int i = 0; i < years; i++ )
{
final_multiplier *= (1 + rate);
}
return base_val * final_multiplier;
}
int main ()
{
double base_val;
double rate;
int years;
cout << "Enter a base value: ";
cin >> base_val;
cout << "Enter an interest rate: ";
cin >> rate;
cout << "Enter the number of years to compound: ";
cin >> years;
cout << "After " << years << " you will have " << computeInterest( base_val, rate, years ) << " money" << endl;
}
I'm using the latest version 13.12 of Code:Blocks, g++ (GCC) 4.8.1 and GNU gdb (GDB) 7.6.1.
我使用的是最新版本的13.12代码:块、g++ (GCC) 4.8.1和GNU gdb (gdb) 7.6.1。
1 个解决方案
#1
3
i
is defined and exists only inside that scope (the for loop):
定义并仅存在于该范围内(for循环):
for ( int i = 0; i < years; i++ )
{
final_multiplier *= (1 + rate);
}
So once you step into one of these lines you will be able to inspect 'i'. And once you are out of this loop, i
will be unavailable again.
所以一旦你进入其中一条线,你就可以检查“i”了。一旦你脱离了这个循环,我将再次无法使用。
EDIT:
编辑:
Make sure that you build with no optimization (-O0) and with debug information (-g) compilation flags.
确保在没有优化(-O0)和调试信息(-g)编译标志的情况下构建。
EDIT2:
EDIT2:
It also might be the GDB issue. Because I also worked with GDB v7.5.1, if I remember correctly, and had the same behavior. Maybe on a higher version of GDB you will get the expected behavior. As Baum mit Augen mentioned in the comments below, GDB v7.9 behaves as you expect.
它也可能是GDB的问题。因为我也使用了GDB v7.5.1,如果我没记错的话,也有同样的行为。也许在更高版本的GDB上,你会得到预期的行为。正如在下面的评论中所提到的,GDB v7.9的表现正如您所期望的那样。
#1
3
i
is defined and exists only inside that scope (the for loop):
定义并仅存在于该范围内(for循环):
for ( int i = 0; i < years; i++ )
{
final_multiplier *= (1 + rate);
}
So once you step into one of these lines you will be able to inspect 'i'. And once you are out of this loop, i
will be unavailable again.
所以一旦你进入其中一条线,你就可以检查“i”了。一旦你脱离了这个循环,我将再次无法使用。
EDIT:
编辑:
Make sure that you build with no optimization (-O0) and with debug information (-g) compilation flags.
确保在没有优化(-O0)和调试信息(-g)编译标志的情况下构建。
EDIT2:
EDIT2:
It also might be the GDB issue. Because I also worked with GDB v7.5.1, if I remember correctly, and had the same behavior. Maybe on a higher version of GDB you will get the expected behavior. As Baum mit Augen mentioned in the comments below, GDB v7.9 behaves as you expect.
它也可能是GDB的问题。因为我也使用了GDB v7.5.1,如果我没记错的话,也有同样的行为。也许在更高版本的GDB上,你会得到预期的行为。正如在下面的评论中所提到的,GDB v7.9的表现正如您所期望的那样。