c++ - Array "breaks" when resizing -


itemprop = "text">

OK, I have a class which is supposed to have a container for quadratic multi-dimensional post (which is a template and I use I'm swimming there, but nothing should be more than that). I used it on a dynamic array, and I was considered overload + and - operator ... there is no problem, right? Okay, it seemed all good and dandy until I really did not let it run.

  listf listf :: operator + (listf rhs) {listf newlist; For (int i = 0; i & lt; elementcount; ++ i) newlist.add (array [i]); For (int j = 0; j & lt; rhs.elementcount; ++ j) newlist.add (rhs.array [j]); Std :: Court & lt; & Lt; newlist; Return new list; }   

Nothing more should be done properly, should not it? That coat is only to check whether it works. But this is not something that should be done, is not it? One of which is the list:

  X ^ 2 + 5x + 52.4 2x ^ 2 + 7x-12   

and the second one is just X ^ 2 + 2 X + 1, it should display a list and:

  X ^ 2 + 5x + 52.4 2x ^ 2 + 7x-12x ^ 2 + 2x + 1   

Buuuut No, it comes this:

  -1.5584e + 038X ^ 2-1.5584e + 038x-1.5584e + 038 -1.5584e + 038X ^ 2-1.5584e + 038x-1.5584e + 038 -1.5584e + 038X ^ 2-1.5584e + 038x-1.5584e + 038   

I have not been battling with it for the last few days and have not been found is . The reason is that what to do

Adding a new multi-dimensional term for the code is still quite simple:

  add zero listf :: adding (polynomial & float & gt; plnm) {if (array = = NULL) {++ elementcount; Array = new polynomial & lt; Float & gt; [Elementcount]; Array [0] = plnum; } Else {array = resize (array, elementcount + 1, elementcount); Array [elementcount ++] = plnm; }}   

And there is a private function in this class resize:

  polynomial & lt; Float & gt; * listf :: resize (polynomial & lt; float & gt; * arrival, int nutrition, int ornas) (polynomial & lt; float> * newArr = new polynomial & lt; float & gt; [newSize]; For (int i = 0; i   

If we are making a small array (to remove objects), then I just like the new size of the old size (I know this is a bad practice and is misleading to others, Things were making the moment: ()

I am out of ideas. Adding new elements in one thing seems to work, but when I want to add two objects, it breaks, elements are broken Prints, which is not correct and then crashes with codeline reporting Anything like "ntdll! LdrVerifyImageMatchesChecksumEx" in the call stack, even better, when I just tested, it displays the correct value, but still changes crash into Rst Done.

Forget a dynamic array of home and use vector. Whenever you have to go into the area of ​​sections and memory management, there is nothing as trivial as the new up-coding [] and delete [] calls. It can stop you dead in your tracks in the development of your program.

  #include & lt; Vector & gt; // ... typedef std :: vector & lt; Polynomial & lt; Float & gt; & Gt; PolynomialFloatArray; // ... class listf {PolynomialFloatArray array; // ... zero addition (const polynomial & lt; float & gt; & amp; plnm); // ... Paleolomial Fluorescence & amp; Resize (PolynomialFloatArray & amp; arr, int newSize)}; // ... zero listf :: add (const polynomial & lt; float & gt; & amp; plnm) {array.push_back (plnm); } Palenomial Flowrate and amp; listf :: resize (PolynomialFloatArray & arr, int newSize) {arr.resize (newSize); Return arr; }   

In short, you have written that all code has reduced 2 or 3 lines

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -