I'm new bee in c ! I'm to iterate over integers using the for loop, but getting the error
error: ‘frame’ was not declared in this scope
auto position_array = (*frame)[i][j];
But as you can see in the code below it is declared
auto ds = data_file.open_dataset(argv[1]);
// auto frame = data_file.read_frame(ds, 0); // inside the loop
for (int i = 0; i < 3; i)
auto frame = data_file.read_frame(ds, i);
for (size_t i = 0; i < nsamples; i) {
for (size_t j = 0; j <= 2; j) { // j<=2 assign all columns
auto position_array = (*frame)[i][j];
}
corr.sample(frame);
}
corr.finalise();
It works fine if I use the commented second line. But now I want to iterate over the second variable of data_file.read_frame(ds, i) and the error comes up! What am I doing wrong? Do I need to declare int frame = 0; before the for loop? For the brevity I just post the code with the error, incase some one need to see the whole code you're welcome!!
CodePudding user response:
Sounds like you need a nested for loop. Using
for (int i = 0; i < 3; i)
{
auto frame = data_file.read_frame(ds, i);
for (size_t j = 0; j < nsamples; j) {
for (size_t k = 0; k <= 2; k) { // j<=2 assign all columns
auto position_array = (*frame)[i][j];
}
corr.sample(frame);
}
}
Lets you get each frame
, and then process each element of each frame.
CodePudding user response:
This for loop
for (int i = 0; i < 3; i)
auto frame = data_file.read_frame(ds, i);
is equivalent to
for (int i = 0; i < 3; i)
{
auto frame = data_file.read_frame(ds, i);
}
That is the sub-statement of the for loop forms its one scope. And outside the scope the variable frame is not visible.
Moreover the variable frame
is created anew in each iteration of the loop.
From the C 17 Standard (9.4 Selection statements)
- ...The substatement in a selection-statement (each substatement, in the else form of the if statement) implicitly defines a block scope (6.3). If the substatement in a selection-statement is a single statement and not a compound-statement, it is as if it was rewritten to be a compound-statement containing the original substatement.