Thursday 15 September 2011

c++ - Passing a pointer through multiple functions and getting segfault -


I am trying to create a framework for the room in a text game practice. The way the game works is that the rooms are classrooms, which have pointers on arrays of tiles pointers, and each tile has an indicator for a container that represents the item on the tile.

This implementation is very easy, and I'm having some problems compiling it properly, however, when I type ' tile on a tile Trying to 'place' it is done by passing a pointer for a Thing object through multiple password functions. The placeOnTile (Thing * i) function of the tile is passed, which gives it to the addItem (Thing * th) function of the tile container, which is a simple test Runs it to make sure it fits into the container (compare to a maxsize int ), then it gets right if it fits.

According to the debug clock, the indicator (name placeholder ) is not changing by pass (this is good). However, when it reaches the final passthrough function (container's addItem (Thing * th) ), it will be a default and will not release the program.

The relevant code sample I could think was listed below if I have more then please let me know.

In the main:

  cout & lt; & Lt; "Bedroom Demo" & lt; & Lt; Andal & lt; & Lt; Endl; Cout & lt; & Lt; "Making bedroom oz ..."; Bedroom B1; // It calls the constructor for a bedroom cot & lt; & Lt; "done." & Lt; & Lt; Andal & lt; & Lt; Endl; In bedroom,   

:

  bedroom () // constructor {Mainine Square (1); // It creates 9 tile pupils, which is arranged in a 3x3 grid thing * placer; // This will indicate objects that you will make placer = new Bed ("your bed", "this is your bed"), false, false, true, 1); // Constructor TI [2] - & gt; Place otile (placer); // !!!! This is where the error occurs !!!! Placer = new decoration ("window facing north", "window of the north is visible in wasteland", false, false, true); Ti [1] - & gt; PlaceOnTile (placer); Placer = New desk ("Your desk", "Your desk is wood and ancient.", 0,0,1,5); Ti [3] - & gt; PlaceOnTile (placer); Remove placeer; // for memory leak}   

h:

  bool placeOnTile (Thing * i) {onTile-> gt; AddItem (i); }   

in the container. H. (The total is a container object explained in the tile):

  bool addItem (Thing * th); Container CPP  
  bool container :: addItem (talk * th) {if (numItems & lt; maxSize) {content [++ num_times] = th; Back true; } And false return; }   

As I mentioned above, a debug clock shows that every step of 'passing' works fine except for the lastest pastry (container). What am I doing wrong?

Note: TI has been declared inside the bedroom, it has an array of 9 tiles, 0 to 8, which make the 'cell' function makeNineSquare is only a function Enables a two-dimensional link list on the array, creates NESW Points in adjacent tiles. Due to this I have created it in such a way that the placement of fixed tiles with the array is provided (as shown in the code), and to simplify easy traversal with signals from the object of the grid (like the player) for.

This allows for the determination of global, general move commands ( moveN just curr = curr-> getN What, say, according to your code, I do not know which points are there, but check whether Numitems is generating an array outside the ++ index.

No comments:

Post a Comment