R
R
Roman Khodakovsky2018-05-19 22:35:41
Arduino
Roman Khodakovsky, 2018-05-19 22:35:41

When initializing an array of strings in an object, the error "too many initializers for 'String[0]'" appears. What could be the problem?

When initializing an array of strings inside an object, the error "too many initializers for 'String [0]'" occurs at the compilation stage and swears just at the array.
Moreover, if the same array is declared in the global space, then everything works.
Here is a sample code

class Colors
{

  public:
    // Список цветов
    String colors[] = {
        "#00ff0d", 
        "#b71c1c", 
        "#9c27b0", 
        "#fbc02d", 
    };

    int _index = 1;      // Текущий номер цвета
    int _colorsSize = 0; // Количество цветов

    String init()
    {
        
    }

    String getColor()    {
       
    }

    String nextColor()    {
    }

    String prevColor()    {
    }
};

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly, 2018-05-19
@h_roman

Because it is forbidden by the standard. Specify the size of the array, or use std::array.
I advise you to add the following compilation flags: -pedantic -Wall -Wextra.

V
vanyamba-electronics, 2018-05-21
@vanyamba-electronics

String colors[4] = {
        "#00ff0d", 
        "#b71c1c", 
        "#9c27b0", 
        "#fbc02d", 
    };

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question