Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Program can't find suitable begin function in nested range-based for loop without using auto

So I’m reading C++ Primer, and I arrive at exercise 3.43 in which I’m supposed to make a program that prints all the elements in an array without using auto – I’m pretty sure this kinda goes against the point of using a range-based for loop, but I think the goal is to understand and learn to explicitly declare confusing variables such as pointers and references. So I write the program:

#include <iostream>

char ia[3][4] = {{'q', 'w', 'e', 'r'}, {'t', 'y', 'u', 'i'}, {'o', 'p', 'a', 's'}};

int main ()
{
    for (char &row : *ia)
        for (char &col : &row)
            std::cout << col << std::endl;
    return 0;
}

But even thought the first for loop seems fine, the second, innermost loop claims there is no suitable begin declaration and the program won’t compile. Why?

I’m still new to range-based for loops, but if my understanding is correct: I need to pass row and col as references so they don’t get automatically converted to pointers by the compiler because that’s how arrays work (how quirky!), and similarly since ia would be passed as a pointer to its first element I need to dereference it so it gets passed as the actual element of the array (which is another array since that’s how multidimensional arrays work).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

That way I’m essentially telling the program: for every row in the array, go through this column, and for every column, (since each column contains one element) print this column.

But the program still insists:

"begin was not declared for this scope."

Is there something wrong with my declarations?

>Solution :

The problem:

There are 2 issues in your code, both in the first loop:

  1. The whole 2D array is ia not *ia.
  2. The type of the ranged loop element is char[4].

A fixed version:

#include <iostream>

int main()
{
    char ia[3][4] = { {'q', 'w', 'e', 'r'}, {'t', 'y', 'u', 'i'}, {'o', 'p', 'a', 's'} };

    using CharArr4 = char[4];
    for (CharArr4 const& row : ia) 
    {
        for (char const& col : row) 
        {
            std::cout << col << std::endl;
        }
    }
    return 0;
}

Notes:

  1. I used a using statement to define the char[4] type to make it more readable.
  2. I used const for the types of the elements of the loop since you do not modify them.
  3. There’s no reason for ia to be global (although in this case it can work as well).

Output:

q
w
e
r
t
y
u
i
o
p
a
s

Live demo – Godbolt

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading