0

I am attempting to initialize a 2D std::array using arrays in C++:

const array<bool, 7> LEDZERO = { 1,1,1,0,1,1,1 };
const array<bool, 7> LEDONE[] = { 0,0,1,0,0,1,0 };
const array<bool, 7> LEDTWO[] = { 1,0,1,1,1,0,1 };
const array<bool, 7> LEDTHREE[] = { 1,0,1,1,0,1,1 };
const array<bool, 7> LEDFOUR[] = { 0,1,1,1,0,1,0 };
const array<bool, 7> LEDFIVE[] = { 1,1,0,1,0,1,1 };
const array<bool, 7> LEDSIX[] = { 1,1,0,1,1,1,1 };
const array<bool, 7> LEDSEVEN[] = { 1,0,1,0,0,1,0 };
const array<bool, 7> LEDEIGHT[] = { 1,1,1,1,1,1,1 };
const array<bool, 7> LEDNINE[] = { 1,1,1,1,0,1,0 };
const array<array<bool, 7>, 10> LEDS = { { {LEDZERO}, {LEDONE}, {LEDTWO},     
{LEDTHREE}, {LEDFOUR}, {LEDFIVE}, {LEDSIX}, {LEDSEVEN}, {LEDEIGHT}, 
{LEDNINE} } };

Only the first LEDZERO seems to be set to LEDS[0] correctly, LEDS[1-9] are wrong.

2
  • 3
    const array<bool, 7> LEDONE[] = { 0,0,1,0,0,1,0 }; -- See anything wrong with using []? The strange thing is that you didn't make the mistake with LEDZERO. Commented Dec 13, 2017 at 3:38
  • Oh I see, I had been trying to work with c-style arrays for a few hours and switched over to std::array. After some sleep I can see how obvious my mistake was! Commented Dec 13, 2017 at 15:03

1 Answer 1

2

You have spurious [] after most LED variables. This will create a C-style array of std::array (probably not what you want).

#include <array>

int main()
{
    const std::array<bool, 7> LEDZERO = {{ 1,1,1,0,1,1,1 }};
    const std::array<bool, 7> LEDONE = {{ 0,0,1,0,0,1,0 }};
    const std::array<bool, 7> LEDTWO = {{ 1,0,1,1,1,0,1 }};
    const std::array<bool, 7> LEDTHREE = {{ 1,0,1,1,0,1,1 }};
    const std::array<bool, 7> LEDFOUR = {{ 0,1,1,1,0,1,0 }};
    const std::array<bool, 7> LEDFIVE = {{ 1,1,0,1,0,1,1 }};
    const std::array<bool, 7> LEDSIX = {{ 1,1,0,1,1,1,1 }};
    const std::array<bool, 7> LEDSEVEN = {{ 1,0,1,0,0,1,0 }};
    const std::array<bool, 7> LEDEIGHT = {{ 1,1,1,1,1,1,1 }};
    const std::array<bool, 7> LEDNINE = {{ 1,1,1,1,0,1,0 }};
    const std::array<std::array<bool, 7>, 10> LEDS = {{
            {LEDZERO},
            {LEDONE},
            {LEDTWO},     
            {LEDTHREE},
            {LEDFOUR},
            {LEDFIVE},
            {LEDSIX},
            {LEDSEVEN},
            {LEDEIGHT}, 
            {LEDNINE}
        }};
}

If you do not need LEDZERO through LEDNINE, you could write it more compact.

#include <array>

int main()
{
    const std::array<std::array<bool, 7>, 10> LEDS = {{
            {{ 1,1,1,0,1,1,1 }},
            {{ 0,0,1,0,0,1,0 }},
            {{ 1,0,1,1,1,0,1 }},
            {{ 1,0,1,1,0,1,1 }},
            {{ 0,1,1,1,0,1,0 }},
            {{ 1,1,0,1,0,1,1 }},
            {{ 1,1,0,1,1,1,1 }},
            {{ 1,0,1,0,0,1,0 }},
            {{ 1,1,1,1,1,1,1 }},
            {{ 1,1,1,1,0,1,0 }}
        }};
}
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.