Saturday 15 February 2014

insert method for an array of objects c++ -


I have an array of integer data like value and I need to insert a new Integerdata in the array. I have thought of creating a temporary array to copy all the contents and then creating a new array with a size + 1 root, but I am getting many errors. Any help? class completion data: public data {public: intestine value; // It is the syntax for a constructor that starts the integer data value for the parameter // property value: value {}} class ArrayCollection: collection {// value of value is always equal to the calculation data * * Values; integer number; Public: ArrayCollection () {// array zero to this-> value = faucet; This- & gt; Number = 0; } ~ ArrayCollection () {// Here the array allocated internally here must be cleaned if (values! = NULL) {delete values]; }} / ** * Calculates the number of elements in the collection * / integer size () const {return calculation; } / ** * Returns the data value on the specified index. If index & gt; = Size () then * NULL is returned * / data * get (int index) {if (index> = size ()) {return NULL; } And {return value [index]; }} ???? - I need help for this method - // I try to allocate the recurrent dynamically, but I am getting an error message: // can not enter the intangible type of data 'data' zero (data * Other) {Count ++; Data ** tempArray = new data [count]; For (int i = 0; i Insert (new rounded data (0)); } Else {for (int i = 1; i & lt; argc; i ++) {// Read user input for integer value int x = atoi (argv [i]); // Put it into our archive-> Insert (new round the data (x)); }} // Printing & lt; & Lt; Collection-> ToString () & lt; & Lt; Endl; // Member IntegerData * 5 = Check the implementation of new IntegerData (5); Cout & lt; & Lt; "Is a member of the five collections?" & Lt; & Lt; Collection-> Member (5) & lt; & Lt; Endl; // Now we are going to insert and remove some items - Marker (A) FullData * v0 = (FullNumber *) Archive-> Obtain (0); COLLECTION- & gt; Removal (v0); Cout & lt; & Lt; Collection-> ToString () & lt; & Lt; Endl; After removing // 0th element, check-marker (B) Cot & lt; & Lt; "Is a member of the five collections?" & Lt; & Lt; Collection-> Member (5) & lt; & Lt; Endl; COLLECTION- & gt; Insert (V0); Cout & lt; & Lt; Collection-> ToString () & lt; & Lt; Endl; // After entering the Element Back Cat, check it & lt; & Lt; "Is a member of the five collections?" & Lt; & Lt; Collection-> Member (5) & lt; & Lt; Endl; // Removed clean memory five; // We should delete allocated data examples allocated in main // because they are compiled by data structure (int i = 0; i & lt; archive-> size (); i ++) {delete collection-> gt; ; I); } // Now delete the data structure - MARKER (c) delete the archive; }

Since you are using C ++, I instead have a vector I recommend using:

  std :: vector & lt; Data * & gt; Vect; ... enter zero (data * other) {vect.push_back (other); }   

Otherwise in C, the function can behave like this:

(assuming that the way you have values ​​ Arrays were allocated in the past, the space does not allow for dynamic resizing ...)

  • Create a new temporary array of values ​​ values_tmp (count ++) <
  • Store the value
  • Store the value: values_tmp [count] = Other;
  • end
  • Delete value with new array: value = values_tmp;

No comments:

Post a Comment