Saturday 15 September 2012

Error when allocating a two dimentional array of an Object in C++ -


I am creating a square called particle matrix which stores two dimensional array of object ball. I want to allocate space for them. The code looks like this.

  / * * Allocated strip takes a width w, allocates space for height, restlength RL * and creates 2D array of sub-orbit ball particles. * / Zero particle matrix :: Alok particles (int w, int h, float rl) {// xDirection xPart = getNrPart (w, rl) is the number of particles; // yDirection yPart = getNrPart (h, rl) is the number of particles; // assigns a line of pointers to pointers from BallArray = new ball * [xPart]; // particle ID int id = 0; For every particle // // for xDirection (int x = 0; x & lt; xPart; x ++) allocate a line of // // Ball Points yPart for a long long ball [x] = new ball [yPart] ; // Create a ball ball array [x] [y] = ball (id, rl * (float) x) for all assigned space (int y = 0; y & lt; ypart; y ++) RL * (float) y); ID ++; }}}   

The problem is with "ballarre [x] = new ball [yPart]". CodeBlocks gives me a compiler error "Error: There is no match for calling 'ball: ball ()' '. I have 4 signatures for different signature balls, no one likes:" Ball () ".

I tried adding a constructor" ball :: ball () "and it compiles, but I think I should be able to allocate space for just one object And then have to instantiate them.

What do I think: Why can not I place the object for a ball without a constructor: Ball :: Ball () "In the code above? And: If some way of assigning space without the constructor "ball :: ball ()" is possible, how would I have to do it?

I know I can make the manufacturer "ball :: ball ()" and give some dummy value to the objects and then set them in their required values, but I feel uncomfortable doing this Happens because I do not know how I can not just "allocated space -> quick objects" I hope I can explain my issue. Thanks!

instead of new t , which calls memory and ctor , You can make a new call to the operator the size you supply only provides you with no memory and then you can enter the placements new Call, which will only bring your CTOR. Instead of assigning you anew, search the Google for available terms to see examples on the location you posted.

But generally you can not do this, your work std :: vector & lt; Ball & gt; with less effort and more security.

No comments:

Post a Comment