Home > Software engineering >  New C 11 for loop causes "error: ‘begin’ was not declared in this scope" using it for an
New C 11 for loop causes "error: ‘begin’ was not declared in this scope" using it for an

Time:07-26

I'm having a struct with an array which is static and want to access the data in that array. Tried to use the new C 11 for loops and also do it without the for loop and just print array[1]. See the second cout in the main function.

I already know that the problem have to do something with the fact that the array in the struct tCalcAngularEstimationFromComplexData_32 is adjusted as a pointer type. Therefore I tried to use for (const auto &arr : &test_data_1) but wasn't able to solve it. Thanks.

typedef struct tComplex_R {
    float real;
    float imag;
}tComplex_R;

typedef struct tOutputVRx_R {
    float range;
    float vel;
    tComplex_R AmpLinRx[1];
} tOutputVRx_R;

typedef struct tCalcAngularEstimationFromComplexData_32{
  int antennas_azimuth;
  int antennas_elevation;
  tOutputVRx_R dataset1[32]; //Range Velocity and Complex Data for 32 Antennas
} tCalcAngularEstimationFromComplexData_32;

static tCalcAngularEstimationFromComplexData_32 tCalcAngEstComplexData_dataset_1 = {
       5, 90, 
       {{1, 3, {10, 15}}, 
        {2, 4, {11, 16}}       
       }         
};


int main(){

tCalcAngularEstimationFromComplexData_32 test_data_1 = tCalcAngEstComplexData_dataset_1;

cout << "Range 1: " << test_data_1.dataset1->range << endl;
cout << "Range 2: " << test_data_1.dataset1[1]->range << endl; //Not working

for (const auto &arr : test_data_1) {
    cout << "Range: " << arr.dataset1->range << endl;


   }
}

CodePudding user response:

Range-based for loops need to have access to the bounds of what you're iterating.

As noted on the relevant cppreference page, there are three ways:

  1. It is an array of known size (which is the case for your dataset1 member)
  2. my_struct::begin() and my_struct::end() are defined
  3. begin(my_struct&) and end(my_struct&) are defined

As for this line:

cout << "Range 2: " << test_data_1.dataset1[1]->range << endl; //Not working

It is because dataset1[1] is not a pointer but a reference, so you don't use -> but . instead.

See a working example on godbolt

  • Related