Error with C array - Norwegian

Asked

Viewed 51 times

0

I’m having error signing an array:

Menu m = Menu();
m.Options[4] = {"teste1","teste2","teste3","teste4"};//Erro aqui
m.Show();

Menu Class:

class Menu
{
public:
    String Options[];
    int index = 0;
    int len = 0;
    String Show()
    {
        while(controller.Button == 0)
        {
        len = sizeof(Options)/sizeof(Options[0]);
            index = index + controller.Left;
            if (index < 0)
            index = len;
            if (index > len)
            index = 0;
            display.setCursor(0,00);
            Clear();
            display.println(Options[index]);
            DrawBuffer();
        }
        return Options[index];
    }
 };

I’ve already tried: m.Options[] = {"teste1","teste2","teste3","teste4"}; and m.Options = {"teste1","teste2","teste3","teste4"};, Didn’t work either.

This is the mistake: no match for 'operator=' (operand types are 'String' and '<brace-enclosed initializer list>')

I’m a beginner in C.

  • How the class was defined Menu ?

  • @Isac I just included the Menu class in the question

  • 1

    When an array is statically defined it must have the elements assigned at that time and/or the size. To be assigned later it has to be set static and instantiated with malloc or new which will be more appropriate for your example since it is C++ and not C. And even the size will have to be saved as you will not be able to know it later. I answered a question just yesterday with a similar problem

1 answer

1

You cannot explicitly set a fixed size for a dynamic array. It is an array rule for both C and C++

Ex of errors:

int array[]; //eu sou dinâmico

main(){
   array[4] = {1,2,3,4}; //definindo tamanho para um array dinâmico? isso tá errado :/

cin << array; //recebendo valores de forma dinamica

maybe that’s possible array[]= {valores};

I haven’t worked with arrays in a while, but I remember that rule.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.