简体   繁体   中英

Braces around scalar initializer error in C++

I am trying to make a simple "font" for numbers to use on my 8x8 display through Arduino. This is just a simple test.

#include <iostream>
using namespace std;

class Font{
    public:
        void Number(int num){
            switch(num){
                case 0:
                    bool Font[][5] = {{{{ 0, 1, 0,
                                      1, 0, 1,
                                        1, 0, 1,
                                         1, 0, 1,
                                          0, 1, 0 }}}};
                   break;

               for(int y = 0; y < 5; y++){
                    for(int x = 0; x < 3; x++){
                        cout << Font[y][x];
                    }
                    cout << endl;
            }
        }
    }
};

int main()
{
    Font myFont;
    myFont.Number(0);

    return 0;
}

However, when I run this, I get an error saying "13:34: error: braces around scalar initializer for type 'bool' 0, 1, 0 }}}};"

That's not how you initialize a multidimensional array. You can either use

bool Font[][5] = {{ 0, 1, 0 },
                  { 1, 0, 1 },
                  { 1, 0, 1 },
                  { 1, 0, 1 },
                  { 0, 1, 0 }};

or

bool Font[][5] = {0, 1, 0,
                  1, 0, 1,
                  1, 0, 1,
                  1, 0, 1,
                  0, 1, 0};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM