I need an explanation for the error in this C++ script

Asked

Viewed 101 times

-4

#include <iostream>
#include <string>

struct Brinquedos{

enum Objetos {Bola, Carrinho, Caixa, Dinamite};


void func(int st_Bola, int st_Carrinho, int st_Caixa, int st_Dinamite){

   Bola=st_Bola;
   Carrinho=st_Carrinho;
   Caixa=st_Caixa;
   Dinamite=st_Dinamite;

    }

};

int main(){

    Brinquedos Insere;
    Brinquedos Mostra;

    Insere.func(10,9,15,25);

    std::cout << Mostra.Bola << Mostra.Carrinho << Mostra.Caixa << Mostra.Dinamite;



}

The error is as follows:

In Member Function 'void Toys::func(int, int, int, int)': 11:12: error: lvalue required as left operand of assignment 12:16: error: lvalue required as left operand of assignment 13:13: error: lvalue required as left operand of assignment 14:16: error: lvalue required as left operand of assignment

2 answers

8

C++ is not a language of script. This code does not make sense, I think you do not want to use an enumeration. If you want, everything else is wrong. Actually I think you want to create a builder so it would be like this:

#include <iostream>
#include <string>

struct Brinquedo {
    int Objetos;
    int Bola;
    int Carrinho;
    int Caixa;
    int Dinamite;
    void func(int st_Bola, int st_Carrinho, int st_Caixa, int st_Dinamite) {
        Bola = st_Bola;
        Carrinho = st_Carrinho;
        Caixa = st_Caixa;
        Dinamite = st_Dinamite;
    }
};

int main() {
    Brinquedo brinquedo;
    //não fazia sentido ter as duas variáveis aqui, cada uma fazendo algo totalmente diferente
    brinquedo.func(10,9,15,25);
    std::cout << brinquedo.Bola << brinquedo.Carrinho << brinquedo.Caixa << brinquedo.Dinamite;
}

I’ve made some cosmetic changes that help make the code legible.

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

3

error: lvalue required as left operand of assignment

This means that you are trying to save a number in a place that is impossible to save. It turns out that enum is to create enumeration, that is, you will create constants with names and that can be sequenced.

For example,

enum DiaDaSemana { dom , seg , ter , qua , qui , sex , sab } ;

is enumeration of days of the week, where DiaDaSemana::dom represents Sunday, is the first and therefore is the smallest, do (int)(DiaDaSemana::dom) returns 0. They follow a sequence: DiaDaSemana::dom is zero and Sunday, DiaDaSemana::seg is one and two, DiaDaSemana::ter it’s two and Tuesday, from then on.

That means trying to do DiaDaSemana::dom = 1 ; is the same thing as trying to set the zero constant equal to one, which is semantic error because it is incoherent. Therefore, in your case the scheme is the same. Besides you don’t use Objeto::Bola but yes Bola (which, if I’m not mistaken, is also a semantic error, but I don’t remember it well), moreover tried to assign value to a constant.

In short, I think you’re interested in creating class object fields, not creating enumeration. If this is the case, recommend you do what the friend suggested, simply change the enumeration

enum Objetos {Bola, Carrinho, Caixa, Dinamite};

by defining object fields

int Bola , Carrinho , Caixa , Dinamite ;

and then you can set your values. I hope to have clarified.

Browser other questions tagged

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