0

The following is my code On line 74 there's an error: lvalue required as left operand of assignement. I believe it is due to the declaration of my operator [], but I can't fix it. Any suggestions? Thanks in advance.

#include<iostream>

using namespace std;

void constructArr(int *arr, int n){
for (int i=0; i<n; i++){
    arr = '\0';
}
}

class vector_int{
public:
int *arr;
int size;
   vector_int(int n){
   size = n-1;
   arr = new int [n];
   constructArr(arr, size);
   }
   vector_int(){
   arr = new int[1];
   size = 0;
   }
   ~vector_int(){
   delete [] arr;
  }
   void push_back(int n){
   int *temp;
   temp = new int [size+1];
   constructArr(temp, size);
   temp = arr;
   temp[size] = n;
   arr = temp;
    size++;
   }
   void pop_back(){
    int *temp;
    temp = new int[size-1];
    constructArr(temp, size-1);
    for (int i=0; i<size-1; i++){
    temp[i] = arr[i];
}
arr = new int [size-1];
arr = temp;
size--;
}
int length(){
return size;
}
int top(){
return arr[size-1];
}
int operator [](int index){
if (index<=size){
    return arr[index];
}
else if (index<0 || index>size-1){
    return arr[size-1];
}
}
int& operator[](int index)const{       //how to correct this?
if (index>size-1 || index<0){
    return arr[size-1];
}
else{
    return arr[index];
}
}
};

int main(){
vector_int test(10);
for (int i=0; i<10; i++){
test[i] = i;  //compiler returns error here
}
    for (int i=0; i<10; i++){
    cout<<test[i];
}
}
  • Sort of off topic: You might find this answer helpful: http://stackoverflow.com/questions/7758580/writing-your-own-stl-container/7759622#7759622 – user4581301 May 03 '16 at 18:45

3 Answers3

0
vector_int test(10);

This is a non-const instance of the class.

test[i] = i;

This is calling the non-const operator[] overload which returns int. Note, this is not int& so you're trying to assign a value to a temporary which is causing the problem (i.e., a temporary is an rvalue and not an lvalue).

Another oddity is that your const overload for operator[] returns int& which means does allow the internal state to be modified, which is contradictory to declaring the member function const.

You should use:

int& operator[](int) { ... }
const int& operator[](int) const { ... }

Or possibly:

int& operator[](int) { ... }
int operator[](int) const { ... }
James Adkison
  • 9,412
  • 2
  • 29
  • 43
0

Your error is not on line 61, it's on line 53, it should be:

int &operator [](int index){

You got the error because you have to pass that value by reference. This makes sense as you will always be modifying that object in some way when you overload the operator.

Nikos Kazazakis
  • 792
  • 5
  • 19
0

int& operator[](int index) const makes no sense. Keyword const means the operator won't modify the object. However, by returning a non const reference, it indirectly allows caller to modify the object....

If you meant to declare an operator to access vector elements in read-only, you need either to return a const reference: const int& operator[](int index) const or a copy int operator[](int index) const. Then you guarantee that using operator[] on a const object won't modify it.

If you meant to declare an operator to access vector elements in read and write, you need to remove const keyword: int& operator[](int index) const.

Commonly, your class should declare both a read-only (const method, returning copy or const reference) and read/write (non const method, returning non const reference) operator[].

jpo38
  • 20,821
  • 10
  • 70
  • 151