Code error: lvalue required as left operand of assignment

1

Why am I getting this:

lvalue required as left operand of assignment

In this line of code:

original[0] = 100;

The code I have is this:

#include <iostream>
#include <queue>
#include <list>
using namespace std;

struct barco
{
    int personas[10];
    int operator[](int c)
    {
        return personas[c];
    }
};


int tamanio = 10;
queue < list <barco> > cola;
list <barco> visitados;
int perm[12][2];

barco original;
barco obj;

int arre[4];

void arregloInicio() {
    original[0] = 100;
    original[1] = 52;
    original[2] = 46;
    original[3] = 49;
    original[4] = 0;
    original[5] = 0;
    original[6] = 0;
    original[7] = 0;
    original[8] = 0;
    original[9] = 0;        
}
    
asked by Roberto López 26.11.2018 в 20:47
source

1 answer

3

Let's look at your class:

struct barco {
  int operator[]( int c ) {
    return personas[c];
  }
};

Your operator[]( ) returns per copy ; we could say that it returns a temporary value (a rvalue ); and, coincidentally, int types do not support / have / implement something similar to operator=( int ) &&

To do what you want, just return by reference :

int &operator[]( int c ) {
  return personas[c];
}
    
answered by 26.11.2018 / 21:16
source