title: Using a dynamic array inside a class -- Receiving error at compile time
body: <p><em><strong>QUESTION ANSWERED IN COMMENTS</em></strong>  Because of my reputation, I can't answer it in the regular fashion. I'll add details in an answer later, already addressed in comments. Thanks.<em>*</em>*</p>
  
  <p>Hello all -</p>
  
  <p>As you'll no doubt see based on the question, I'm new to C++, but have experience with some higher level languages. (Which seems to be hurting more than helping) </p>
  
  <p>For a class, I need to create a wrapper for an Array that is typed to integers. (No templates at this stage of class) I also need to allow the class to have a non-zero starting index. I'm using a member array in the class to store my data (no vectors yet at this point of the class) and doing some translation from the public methods to access the appropriate internal array element. </p>
  
  <p>The problem that I'm running into is that I don't know the internal array size at compile time, so I'm declaring it as a class global pointer and setting up the size in the constructor. A code snippet is below in the problem area:</p>
  
  <pre><code>int *list;
  safeArray::safeArray(int start, int initialSize)
  {
      if(initialSize &lt;= 0)
      {
          throw "Array size must be a positive integer";
      }
      maxSize = initialSize + 1;
      startIndex = start;
      endIndex = start + initialSize;
      list = new int[maxSize];    // Error thrown here
      int *tempArray = new int[maxSize];
      copyArray(tempArray);
      clearArray();   
  }
  </code></pre>
  
  <p>The error I'm getting is </p>
  
  <p><code>Incompatible types in assignment of 'int*' to 'int[0u]'</code></p>
  
  <p>I'm not 100% sure what the type of int[0u] is. Is that the literal value zero and the u is for unsigned? I've checked in the debugger that maxSize holds a value and I've also replaced it with a constant integer value and got the same error.</p>
  
  <p>Because my <code>int *tempArray = new int[maxSize];</code>  line worked, I thought it might have something to do with needing to declare and size at the same time, so I opted to do a memcpy. (Which is actually outside the scope of the assignment, so there must be something else I'm missing) The memcpy fails because it appears that I'm clobbering my other variables. When I print the address of list in GDB, it gives me the same address as another global variable in my code, so that route also seemed it was out of the scope of the assignment.</p>
  
  <p>The common theme I've seen in other forums is that you can't assign arrays like other variables, but I didn't think that would include the <code>new</code> statement. Am I wrong in that assumption? </p>
  
  <p>The only compilation errors I'm currently seeing is the one stated above, and I see it for every <code>list = new int[maxSize];</code> statement in the code. </p>
  
  <p>My questions are:</p>
  
  <ol>
  <li><p>What is the type int[0u] and where is that type being generated? It would have to be from the new statement right?</p></li>
  <li><p>What is the best way to utilize a dynamic array resource inside of a class? Besides using a vector? =)</p></li>
  </ol>
  
  <p>I think that's all the relevant info, but my apologies if I missed a crucial piece of data. Below is the rest of the implementation code.</p>
  
  <pre><code>/*
   *  safeArray.cpp
   *  safearray
   *
   *  Created by Jeffery Smith on 6/1/11.
   *  
   *
   */
  
  #include "safeArray.h"
  #include &amp;lt;iostream&amp;gt;
  
  
  using namespace std;
  
  
      int startIndex = 0;
      int endIndex = 0;
      int maxSize = 1;
      int currentSize = 0;
      int *list;
  
  safeArray::safeArray(int start, int initialSize)
  {
      if(initialSize &lt;= 0)
      {
          throw "Array size must be a positive integer";
      }
      maxSize = initialSize + 1;
      startIndex = start;
      endIndex = start + initialSize;
      list = new int[maxSize];    // Error thrown here
      int *tempArray = new int[initialSize + 1];
      copyArray(tempArray);
      clearArray();
  
  }
  
  safeArray::safeArray(const safeArray &amp;sArray)
  {
      list = new int[sArray.maxSize];
      copyArray(sArray);
      startIndex = sArray.startIndex;
      endIndex = sArray.endIndex;
      maxSize = sArray.maxSize;
      currentSize = sArray.currentSize;
  }
  
  void safeArray::operator=(const safeArray &amp;right)
  {
      list = new int[right.maxSize];
      copyArray(right);
      startIndex = right.startIndex;
      endIndex = right.endIndex;
      maxSize = right.maxSize;
      currentSize = right.currentSize;
  }
  
  safeArray::~safeArray()
  {
      delete [] list;
  }
  
  
  
  int safeArray::operator[](int index)
  {
      if(OutofBounds(index))
      {
          throw "You tried to access an element that is out of bounds";
      }
      return list[index - startIndex];
  }
  
  void safeArray::add(int value)
  {
      if(this-&gt;isFull())
      {
          throw "Could not add element. The Array is full";
      }
      currentSize++;
      list[currentSize + startIndex];
  }
  
  void safeArray::removeAt(int value)
  {
      if(OutofBounds(value))
      {
          throw "The requested element is not valid in this list";
      }
      compressList(value);
      currentSize--;
  }
  
  void safeArray::insertAt(int location, int value)
  {
      if(OutofBounds(location) || this-&gt;isFull())
      {
          throw "The requested value is either out of bounds or the list is full";
      }
      expandList(location, value);
      currentSize++;
  }
  
  
  void safeArray::clearList()
  {
      clearArray();
  }
  
  bool safeArray::isFull()
  {
      return(maxSize == currentSize);
  }
  
  int safeArray::length()
  {
      return currentSize;
  }
  
  int safeArray::maxLength()
  {
      return this-&gt;maxSize;
  }
  
  bool safeArray::isEmpty()
  {
      return(currentSize == 0);
  }
  
  bool safeArray::OutofBounds(int value)
  {
      return (value &gt; endIndex || value &lt; startIndex);
  }
  
  void safeArray::clearArray()
  {
      for(int i = 0; i &lt; maxSize; i++)
      {
          list[i] = 0;
      }
      currentSize = 0;
  }
  
  void safeArray::compressList(int value)
  {
      for(int i = value; i &lt; endIndex; i++)
      {
          list[i] = list[i + 1];
      }
  }
  
  void safeArray::expandList(int location, int value)
  {
      int tempHolder = list[location];
      list[location] = value;
      for(int i = location; i &lt; endIndex; i++)
      {
          tempHolder = list[location];
          list[location] = value;
          value = tempHolder;
      }
  }
  
  void safeArray::copyArray(int *srcAddr )
  {
  
      memcpy(list, srcAddr, sizeof(int) * maxSize);
  
  }
  
  void safeArray::copyArray(const safeArray &amp;sArray)
  {
  
      memcpy(list, &amp;sArray, sizeof(int) * maxSize);
  
  }
  </code></pre>
  
  <p>Here is the header definition:</p>
  
  <pre><code>
  /*
   *  safeArray.h
   *  safearray
   *
   *  Created by Jeffery Smith on 6/1/11.
   *  Copyright 2011 Accenture. All rights reserved.
   *
   */
  
  
  
  class safeArray {
  
  public:
      safeArray(int,int);    //Standard constructor
      ~safeArray();          //Destructor
      int operator[](int);
      void operator=(const safeArray&);   //Assignment overload
      safeArray(const safeArray &sArray); //Copy Constructor
  
      void add(int);
      int maxLength();
      int length();
      bool isFull();
      bool isEmpty();
      void clearList();
      void removeAt(int);
      void insertAt(int,int);
  
  protected:
      int list[];
      int startIndex;
      int endIndex;
      int maxSize;
      int currentSize;
  
  private:
      void clearArray();
      bool OutofBounds(int);
      void expandList(int,int);
      void compressList(int);
      void copyArray(int*);
      void copyArray(const safeArray&);
  };
  </code></pre>