c++for-loopscopedeclaration

Variable is not declared in the scope error in c++?


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!!


Solution

  • 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.