title: How to initialize an array in private class without using a constant array size?
body: <p>I have a header and .cpp file. I am having difficulty initializing the array. The problem is that I can't specify the array size. The size depends on the number of elements the user inputs. When I make a new dynamicArray, I believe I have to use the "new" like dynamicArray = new string[sizeof(array)] (because I have to delete the memory later according to the assignment) but when I run it through Linux, it says that it cannot appear in a constant - expression. </p>
  
  <p>I'm still a little unfamiliar with C++. Any feedback is appreciated.</p>
  
  <p>What the error looks like:</p>
  
  <p><a href="https://i.stack.imgur.com/M6zUH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/M6zUH.png" alt="image"></a></p>
  
  <p>I also had issues with the header redefinition and I think the #pragma once helped.
  I know #include "stdafx.h" is bad for linux. I remove it every time I run it on linux.</p>
  
  <p>DynamaicStringArray.cpp</p>
  
  <pre><code>// DynamicStringArray.cpp : Defines the entry point for the console application.
  //
  
  #include "stdafx.h"
  #include "DynamicStringArray.h"
  #include "Assignment9Test_V1.cpp"
  
  // copy constructor that copies the array
  DynamicStringArray::DynamicStringArray(string array[]) {
      // string *dynamicArray = new string[sizeof(array)]; // without pointer, error with conversion between types
      dynamicArray = new string[sizeof(array)];
  
      for (int i = 0; i &lt; sizeof(array); i++) {
          dynamicArray[i] = array[i];
      }
  }
  
  // default constructor
  DynamicStringArray::DynamicStringArray() {
      size = 0;
  
      dynamicArray = NULL;
      // dynamicArray = new string[size];
  }
  
  // frees up dynamic array memory
  void DynamicStringArray::destructor(string array[]) {
      delete[] array;
      array = NULL;
  }
  
  // return number of entries in array
  int DynamicStringArray::getSize() {
      return size;
  }
  
  // creates a new dynamic array one element larger than dynamicArray
  // copies all elements of dynamicArray to new array and
  // add new string to end of new array
  // increment size
  // delete old dynamicArray
  // set new array as dynamicArray
  void DynamicStringArray::addEntry(string newString) {
      string *newArray;
      newArray = new string[size + 1];
  
      for (int i = 0; i &lt; size; i++) {
          newArray[i] == dynamicArray[i];
      }
  
      int endIndex = sizeof(newArray);
      newArray[endIndex] = newString;
  
      destructor(dynamicArray);
      dynamicArray = newArray;
  }
  
  // searches for dynamicArray for specific string,
  // if string not found, return false
  // if string is found, create new one size smaller dynamic array
  // than dynamicArray
  // copy elements of old dynamicArray to new array without the string
  // delete old dynamicArray
  // decrement size
  // return true
  bool DynamicStringArray::deleteEntry(string deleteMe) {
      string *newArray;
      newArray = new string[size - 1];
  
      bool isFound = false;
  
      for (int i = 0; i &lt; size; i++) {
          if (dynamicArray[i] == deleteMe) {
              i++;
              isFound = true;
          }
          else if (dynamicArray[i] != deleteMe) {
              newArray[i] == dynamicArray[i];
          }
      }
  
      if (isFound) {
          return true;
          destructor(dynamicArray);
  
          dynamicArray = newArray;
      }
      else if (!isFound) {
          return false;
      }
  }
  
  // returns the string at that index
  string DynamicStringArray::getEntry(int findMe) {
      return dynamicArray[findMe];
  }
  </code></pre>
  
  <p>DynamicStringArray.h</p>
  
  <pre><code>#pragma once
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  class DynamicStringArray {
  public:
      DynamicStringArray();
  
      DynamicStringArray(string array[]);
  
      // overloading the assignment operator
  
      void destructor(string array[]);
      int getSize();
      void addEntry(string);
      bool deleteEntry(string);
      string getEntry(int);
  
  private:
      int size; // holds number of entries in the array
      string *dynamicArray; // references a dynamic array of type string
  };
  </code></pre>