0

I created this simple array class and it works fine but there is 1 issue.... Everytime i want to insert value at some index then i have to do something like array.insert(0, 10); where 0 is index and 10 is value. Can't i do it something like array[0] = 10; I have already overrided the [] operator to print value like cout << array[0]; but if i try something like array[0] = 10; then it throws an error Expression is not assignable

I wish someone helps me btw here is the code....

#include <iostream>

template<typename type, int size>
 class Array
{
    private:
      type array[size];
    
    public:
    
      int len()  //returns the number of elements
      {
        return size;
      }
      
      type val(int index) //returns the value at given index
      {
        return array[index];
      }
    
      int memsize() //returns the size of array in bytes
      {
        return sizeof(array);
      } 
    
      void pop(int index) //deletes element at given index
      {
        delete array[index];
      }
    
      void insert(int index, type value) //inserts an value at given index
      {
        if(index >= size)
        {
          std::cout << "Error! Cannot insert value at unspecified index";
          exit(0);
        }
        else
        {
          array[index] = value;
        }
      }
    
      void update(int index, type value) //updates the values at given index
      {
        if(index >= size)
        {
          std::cout << "Error! index <" << index << "> not found";
          exit(0);
        }
        else
        {
          array[index] = value;
        }
      }
    
      type operator[](int index) //overriding the [] operator so that we can access an element using [index]
      {
        if(index >= size)
        {
          std::cout << "Error! Index value over limits";
          exit(0);
        }
        else
        {
          return array[index];
        }
      }
};


int main()
{
  Array<int, 3> array;
  array.insert(0, 10);
  array.insert(1, 20);
  
  array[2] = 8; //This line throws Error
  //Error (Expression is not assignable)
 
}

If anyone has any idea to solve this then please help... Thanks in advance to everyone.

CsharpUser
  • 31
  • 4
  • 4
    You should return `type&` from your `operator[]`. – rturrado Dec 31 '21 at 16:47
  • Since your `operator[]` returns a temporary, you are attempting to assign to that temporary. – Drew Dormann Dec 31 '21 at 16:48
  • [Array subscript operator](https://en.cppreference.com/w/cpp/language/operators#Array_subscript_operator) – Evg Dec 31 '21 at 16:49
  • @rturrado Can you please give me an written function because i didn't get your point? – CsharpUser Dec 31 '21 at 17:03
  • Sure: https://godbolt.org/z/zPh1ce5cs (I have just rewritten the `operator[]` method as `type& operator[](int index)` and printed out `array[2]` in `main` after modifying it. The point is that if you return a `type`, you are returning a copy of the vector element; you need to return a reference to the element; a reference is assignable. – rturrado Dec 31 '21 at 17:06
  • Notice though that it won't work for `const Array`s. If you ever face that problem, remind you'll need to provide a `const type& operator[](int index) const` method as well. You can use STL vector's implementation as a [reference](https://en.cppreference.com/w/cpp/container/vector/operator_at). – rturrado Dec 31 '21 at 17:12

0 Answers0