I have a class where I generate new objects and store them in the array. The problem is that, the data of the first object is being overwritten with the other's data as soon as I add the second object. What am I doing wrong? Thanks
rotor rot [100]; // How can I make this flexible? Int main (int argc, char ** argv) {for (int i = 1; i <(argc - 1); i ++) {ifstream infix; InFile.open (argv [i]); If (inFile.fail ()) {std :: invalid_argument ("Error reading file!"); Return 1; } Rot [ii] = rotor (); Int n; While (Infile> gt; & gt; N) {rot} [i] .import (n); }} ...
class rotor:
# include "rotor" int inmap [26]; Int'l Outam [26]; Int exchange; Rotor :: rotor () {impcount = 0; } Zero rotor :: import (int i) {outmap [i] = impcount; Inmap [impcount] = i; Impcount ++; }
now rotor [1]. The uptake [0] 1 is in the beginning, but later I added the rotor [2] it changes to the same value as the rotor [2] . IPod [0] = 2. Is there any way to make Array's length flexible so that it can only be used in many places? This is necessary in later methods.
Thank you
Your rotor
To store the static arrays (shared by all types), this is the reason why every subsequent example of the rotor overwrite appears first one is very bad. And generally you should avoid static (global) data in C ++ programs.
int inmap [26]; Int'l Outam [26]; Int exchange;
You need to create those personal members instead of class:
class rotor {personal: int inmap [26]; Int'l Outam [26]; Int exchange; // other private members here public: // constructor and other public methods here}; To make the array flexible, you can do it by hand (use the array allocated on the heap, and the size of the number of items can change the size of the array - you can double each time the ability to assign a new array , Copying the contents of the old one and removing the original) or very easy and less error prone, use only one dynamic array implementation like std :: vector
.
Comments
Post a Comment