Note that there are some explanatory texts on larger screens.

plurals
  1. POinsert method for an array of objects c++
    text
    copied!<p>I have an array of integer data called values and I need to insert a new Integerdata into the array. I have thought about making a temp array to copy all the contents and then make a new array with a size + 1 the original but I keep getting many errors. any help? </p> <pre><code>class IntegerData : public Data { public: int value; // This is the syntax for a constructor that initializes the // properties value to the parameters IntegerData(int value) : value(value) {} } class ArrayCollection : Collection { // length of values is always the same as count Data ** values; int count; public: ArrayCollection() { // initialize the array to NULL this-&gt;values = NULL; this-&gt;count = 0; } ~ArrayCollection() { // must clean up the internally allocated array here if (values != NULL) { delete [] values; } } /** * Returns the count of the number of elements in the Collection */ int size() const { return count; } /** * Gets the Data value at the specified index. If index &gt;= size() then * NULL is returned. */ Data * get(int index) { if (index &gt;= size()) { return NULL; } else { return values[index]; } } ????-- I need help with this method-- // I try to dynamically allocate tempArray but I get the error message saying: cannot // allocate an object of abstract type 'Data' void insert(Data * other){ count++; Data **tempArray = new Data[count]; for(int i = 0; i &lt; count; i++){ tempArray[i] = values[i]; } delete [] values; values = tempArray; } } int main(int argc, const char * argv[]) { // create an ArrayCollection for our collection of integers ArrayCollection * collection = new ArrayCollection(); if (argc == 1) { // they didn't provide any arguments to the program, insert a // value of zero so that the main function still works collection-&gt;insert(new IntegerData(0)); } else { for (int i = 1; i &lt; argc; i++) { // read user input for integer value int x = atoi(argv[i]); // insert it to our collection collection-&gt;insert(new IntegerData(x)); } } // print the collection cout &lt;&lt; collection-&gt;toString() &lt;&lt; endl; // check the implementation of member IntegerData * five = new IntegerData(5); cout &lt;&lt; "five is a member of collection? " &lt;&lt; collection-&gt;member(five) &lt;&lt; endl; // now we are going to insert and remove a few items -- MARKER (a) IntegerData * v0 = (IntegerData *)collection-&gt;get(0); collection-&gt;remove(v0); cout &lt;&lt; collection-&gt;toString() &lt;&lt; endl; // check after removing the 0th element -- MARKER (b) cout &lt;&lt; "five is a member of collection? " &lt;&lt; collection-&gt;member(five) &lt;&lt; endl; collection-&gt;insert(v0); cout &lt;&lt; collection-&gt;toString() &lt;&lt; endl; // check after inserting the 0th element back cout &lt;&lt; "five is a member of collection? " &lt;&lt; collection-&gt;member(five) &lt;&lt; endl; // clean up memory delete five; // must delete IntegerData instances that we allocated in main // because they are not deleted by the data structure for (int i = 0; i &lt; collection-&gt;size(); i++) { delete collection-&gt;get(i); } // now delete the data structure -- MARKER (c) delete collection; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload