title: Is it possible to assign a user-defined Array by index with operator overloading? - C++
body: <p><hr>
  <strong>The Problem:</strong> When I attempt to assign an IntArray object by index I get the following error:  </p>
  
  <blockquote>
    <p>"Expression is not assignable."</p>
  </blockquote>
  
  <p>The error is produced by the following line of code in iadrv.cpp:</p>
  
  <pre><code>IntArray a(10);
  for(int i = a.low(); i &lt;= a.high(); i++)
      a[i] = i * 10;
  </code></pre>
  
  <p>I am able to assign an entire IntArray object to another like so, <code>a = b;</code>, however when a specific index is referred to the "expression is not assignable" error occurs.</p>
  
  <p><strong>EDIT:</strong> I removed the <code>const</code> declaration from most of the functions and I am not getting the "Expression is not assignable" error anymore. However, the setName now gives the error:</p>
  
  <blockquote>
    <p>"ISO C++ 11 does not allow conversion from string literal to 'char *'"</p>
  </blockquote>
  
  <p>This error is cause by the following code in iadrv.cpp:</p>
  
  <pre><code>a.setName("a");
  </code></pre>
  
  <p></p>
  
  <p><hr>
  <strong>Program Explanation:</strong></p>
  
  <p>I have written a class IntArray (in C++) in which the following operators are overloaded:</p>
  
  <ul>
  <li>"[ ]"  : allows index range checking</li>
  <li>"="    : allows array assignment</li>
  <li>"+"    : allows the sum of two arrays to be assigned to a third array</li>
  <li>"+="   : allows the sum of two arrays to be assigned to the first array</li>
  <li>"&lt;&lt;"   : allows the contents of an array to be output</li>
  </ul>
  
  <p>The program also includes functions:</p>
  
  <ul>
  <li>setName : sets the name of the IntArray object </li>
  <li>getName : returns the name of the IntArray object</li>
  <li>low     : returns the smallest legal index</li>
  <li>high    : returns the largest legal index</li>
  <li>length  : returns the number of elements</li>
  </ul>
  
  <p>A driver program (iadrv.cpp, iadrv.h) will run tests on the IntArray class (IntArray.cpp, IntArray.h) to determine if all operators were properly overloaded.</p>
  
  <p><strong>Note:</strong> that for each array test data, the driver will simply multiply the
  array index by 10 immediately after each array is initialized or modified and output its contents. When the program encounters a run-time error, it should "simulate"a halt with appropriate diagnostics rather than actually halting the program. 
  
  <hr>
  <strong>The Code:</strong></p>
  
  <p>IntArray.h</p>
  
  <pre><code>//  IntArray.h
  
  #ifndef __IntArray__IntArray__
  #define __IntArray__IntArray__
  
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;iomanip&gt;
  
  using namespace std;
  
  class IntArray {
  private:
      int a, b;
      int size;
      int * num;
      char * name;
  public:
      IntArray(int start, int finish);
      IntArray(int finish = 10);
      IntArray(const IntArray &amp;); //constructor copy
      ~IntArray();
      int low() const;
      int high() const;
      char * getName() const;
      //removed the const declaration from functions below
      int &amp; operator [] (int);     //made to return int&amp;
      friend ostream &amp; operator &lt;&lt; (ostream &amp;, IntArray &amp;);
      void setName(char *);
      int length() const;
      const IntArray &amp; operator = (IntArray &amp;);
      const IntArray &amp; operator + (IntArray &amp;);
      bool operator += (IntArray &amp;);
  
  };
  
  #endif /* defined(__IntArray__IntArray__) */
  </code></pre>
  
  <p>IntArray.cpp</p>
  
  <pre><code>//  IntArray.cpp
  
  #include "IntArray.h"
  
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  extern ofstream csis;
  
  IntArray::IntArray(int start, int finish) {
      if (start &gt; finish) {
          cout &lt;&lt; "Simulating a halt.";
          a = finish;
          b = start;
      }
      else {
          a = start;
          b = finish;
      }
      size = b-a;
      num = new int[size];
      name = new char[1];
      for (int i = 0; i &lt; size; i++) {
          num[i] = 0;
      }
  }
  IntArray::IntArray(int finish) {
      size = finish;
      a = 0;
      b = finish - 1;
      num = new int[size];
      name = new char[1];
      for (int i = 0; i &lt; size; i++) {
          num[i] = 0;
      }
  }
  IntArray::IntArray (const IntArray &amp; right): size(right.size) {
      num = new int[size];
      name = new char[1];
      for (int i = 0; i &lt; size; i++) {
          num[i] = right.num[i];
      }
  }
  IntArray::~IntArray() {
      delete[] num;
      delete [] name;
  }
  int IntArray::low() const{
      return a;
  }
  int IntArray::high() const{
      return b;
  }
  char * IntArray::getName() const{
      return name;
  }
  void IntArray::setName(char * n) {
      name = n;
  }
  //removed const declarations
  //made to return int&amp;
  int &amp; IntArray::operator [] (int subscript) const{
      if (subscript &lt; a || subscript &gt; b) {
          cout &lt;&lt; "subscript: " &lt;&lt; subscript &lt;&lt; endl;
          cout &lt;&lt; "Out of bound error. Simulating a halt." &lt;&lt; endl;
          return num[a];
      }
      return num[subscript-a];
  }
  int IntArray::length() const{
      //b-a = size
      return (b-a);
  }
  //removed const declarations
  ostream &amp; operator &lt;&lt; (ostream &amp; output, IntArray &amp; array) {
      for (int i = array.low(); i &lt;= array.high(); i++) {
          output &lt;&lt; array.name &lt;&lt; "[" &lt;&lt; i &lt;&lt; "] = " &lt;&lt; array[i] &lt;&lt; endl;
      }
      return output;
  }
  //removed const declarations
  IntArray &amp; IntArray::operator = (IntArray &amp; right) {
      if (length() == right.length()) {
          for (int i = 0; i &lt;= length(); i++) {
              num[i] = right[right.low()+i];
          }
      return * this;
      }
      else {
          delete [] num;  //reclaim space
          delete [] name;
          size = right.length();
          num = new int [size]; //space created
          cout &lt;&lt; "Different sized arrays. Simulating a hault" &lt;&lt; endl;
      }
      return * this;
  }
  //removed const declarations
  IntArray &amp; IntArray::operator + (IntArray &amp; right) {
      int * ptr;
      ptr = new int [right.length()];
      if (length() == right.length()) {
          for (int i = 0; i &lt; length(); i++) {
              ptr[i] = num[i] + right[right.low()+i];
          }
      }
      return * this;
  }
  //removed const declarations
  bool IntArray::operator += (IntArray &amp; right) {
      if (length() == right.length()) {
          for (int i = 0; i &lt;= right.length(); i++) {
              num[i] += right[right.low()+i];
          }
          return true;
      }
      cout &lt;&lt; "Could not add the sum of the arrays into first array. Simulating a halt." &lt;&lt; endl;
      return false;
  }
  </code></pre>
  
  <p>iadrv.h</p>
  
  <pre><code>//  iadrv.h
  
  #ifndef p6_iadrv_h
  #define p6_iadrv_h
  
  #include "intarray.h"
  
  int main();
  void test1();
  void wait();
  
  #endif
  </code></pre>
  
  <p>iadrv.cpp</p>
  
  <pre><code>//  iadrv.cpp
  
  #include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  #include &lt;fstream&gt;
  #include &lt;stdlib.h&gt;
  #include "iadrv.h"
  
  using namespace std;
  
  ofstream csis;
  
  int main() {
      csis.open("csis.dat");
      test1();
      csis.close();
  }
  
  void test1() {
      system("clear");
      cout &lt;&lt; "1. Array declared with single integer: IntArray a(10);" &lt;&lt; endl &lt;&lt; endl;
      csis &lt;&lt; "1. Array declared with single integer: IntArray a(10);" &lt;&lt; endl &lt;&lt; endl;
      IntArray a(10);
      for(int i = a.low(); i &lt;= a.high(); i++)
          a[i] = i * 10;
      a.setName("a");
      cout &lt;&lt; a &lt;&lt; endl;
      csis &lt;&lt; a &lt;&lt; endl;
      wait();
  }
  </code></pre>
  
  <p></p>
  
  <p><strong>DISCLAIMER:</strong> This program was written as a school assignment and has <em>already been turned in to be graded</em>. This was my first c++ program so I would like to understand my mistakes. Your help is sincerely appreciated. </p>