0

For this assignment i am supposed to use a range for to print out the elements in ia without using the auto keyword. Basically the assignment is trying to help us understand multidimentional arrays. I have an idea of what is going on in the code but i keep running into some errors. There is something wrong with the syntax and i cant figure it out.

int ia[3][4] = {{0,1,2,3},{4,5,6,7},{8,9,10,11}};

cout << endl;
for(int &a : ia)
    for(int b : a)
        cout << b << endl;

I keep getting these errors:

..\src\Sec_3_5_3.cpp:127:15: error: invalid conversion from 'int*' to 'int' [-fpermissive] for(int &a : ia)

..\src\Sec_3_5_3.cpp:127:15: error: cannot bind rvalue '(int)((int*)__for_begin)' to 'int&'

..\src\Sec_3_5_3.cpp:128:15: error: 'begin' was not declared in this scope

..\src\Sec_3_5_3.cpp:128:15: error: 'end' was not declared in this scope

1 Answer 1

2

Each ia[i] is not an int, but an array of 4 ints.
And to be able to keep the size, you have to use a reference:

for(int (&a)[4] : ia)
    for(int b : a)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.