Instancing an Object with a Dynamically created name?

Started by
20 comments, last by stylin 18 years, 5 months ago
Quote:object & get_object( std::vector< object > & cube, int x, int y, int z ) {

return cube[ ( x*HEIGHT + y ) * DEPTH + z ];
}


"it threw a syntax error at me" =
1137 C:\Dev-Cpp\Test.cpp syntax error before `&' token

Not to descriptive is it [rolleyes] those were the only new lines in my code, aside from the declaration. And I meant to say the FPS bombed out, leading to the off-hand question of erasing a portion of a vector without an iterator.
C:DosC:DosRunRunDosRun!
Advertisement
Quote:Original post by Crashman
Quote:object & get_object( std::vector< object > & cube, int x, int y, int z ) {

return cube[ ( x*HEIGHT + y ) * DEPTH + z ];
}


"it threw a syntax error at me" =
1137 C:\Dev-Cpp\Test.cpp syntax error before `&' token

Check if you have object defined. Check if you have #included the standard header <vector>.
Quote:Not to descriptive is it [rolleyes] those were the only new lines in my code, aside from the declaration. And I meant to say the FPS bombed out, leading to the off-hand question of erasing a portion of a vector without an iterator.

No, and I don't think that's what you want to do anyway. In this situation, you want to keep the cube's structure, but also allow some parts of the cube to be empty, right? I would make a cell class that represents a particular location in the cube. These cells will have pointers (boost::shared_ptr) to the objects that they contain.

Think about a garage: at any time a single garage can hold a single car. When that car leaves the garage, the garage becomes empty - but the garage is still there. The cells represent the garages for the storage of objects. When you need to remove an object from the cube, the cell does that for you, that is, it releases the object it was previously holding onto. You can give it a function to tell you if an object is in there or not as well. Such a class might look like:

   class cell {   public:      cell(): pObject(0) {}      ~cell() { delete pObject; }         ...      void store( const object & );   // loads an object into the cell      void clear();                   // releases object from cell      bool empty() const;   private:      object * pObject;               // our object   };


[Edited by - stylin on October 26, 2005 1:12:36 PM]
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid

This topic is closed to new replies.

Advertisement