Assigning objects to objects

This is a discussion on Assigning objects to objects within the C++ Programming forums, part of the General Programming Boards category; I get a fatal error when the following lines are run. foe is an array of objects (representing monsters in ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    25

    Assigning objects to objects

    I get a fatal error when the following lines are run. foe is an array of objects (representing monsters in a game I'm making), nummonsters is the number of monsters in that array. When they are destroyed, I run this loop to remove the dead monsters (by swapping their position with the last one, and reducing the scope of the array).

    Does the following look ok?

    Code:
         for (int i=0; i<nummonsters; i++){
             if (foe[i]->hp<=0) {foe[i]=foe[nummonsters]; nummonsters--; return;}
         }
    I have never used the 'foe[i]=foe[nummonsters]' thing before. Must I manually assign each variable :
    Code:
    foe[i]->a=foe[nummonsters->a;
    foe[i]->b=foe[nummonsters->b;
    foe[i]->c=foe[nummonsters->c;
    etc.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    You'd be better off using a vector rather than an array. Also make them dynamic, so killing them off is as easy as deleting the memory.

    Your error would appear to be that you're assigning to foe[nummonsters] which being a total of the monsters is likely one higher that the last index in the array.
    Sent from my iPadŽ

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Array indices run from 0 to (nummonsters - 1), so accessing foe [nummonsters]
    is out of bounds

    EDIT: Too slow!!!

    >>I have never used the 'foe[i]=foe[nummonsters]' thing before. Must I
    manually assign each variable :

    as far as I know, your compiler should automatically implement an assignment
    operator overload for your class, which in most cases is suitable, so yes this
    should be ok as opposed to assigning every variable

    >>foe[i]->a=foe[nummonsters->a;

    missing square brace, and it's probably meant to be the '.' operator instead of
    the '->' operator. Also, variables in your class should be labelled private,
    allowing access like this breaks the idea of encapsulation.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    25
    Bangs head: yes, the nummonsters was 1 larger than the last element. Thanks for that

    Also, every time I use '.' in place of '->' things just stop working. I prefer '.', but it doesn't seem to work for me.

    I prefer to use the primitive array: there will not need to be that many monsters in play at a time, so I wouldn't save space, and I also think an array is easier to use.

    One more thing: is the way I'm doing it going to cause memory leaks, since I haven't properly deleted the data before overwriting it? I've done most of my OO programming in Java, so am not sure how much memory management C++ does on its own.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    I prefer to use the primitive array: there will not need to be that many monsters in play at a time, so I wouldn't save space, and I also think an array is easier to use.
    A std::vector is likely to be easier than a manually managed dynamic array.

    For example, we can write a function object that tells us if the foe is dead:
    Code:
    // Foe class example
    class Foe
    {
    public:
    	int hitPoints() const;
    	// ...
    private:
    	// ...
    };
    
    // function object class example
    class FoeIsDead
    {
    public:
    	bool operator()(const Foe& foe) const
    	{
    		return foe.hitPoints() <= 0;
    	}
    };
    Now, we can do what you wanted to do, in a single line:
    Code:
    std::vector<Foe> foes; // vector of foes
    // ...
    // remove the foes that are dead
    foes.erase(std::remove_if(foes.begin(), foes.end(), FoeIsDead()), foes.end());
    Instead of having to keep track with a nummonsters variable, we just use foes.size() if necessary.

    One more thing: is the way I'm doing it going to cause memory leaks, since I haven't properly deleted the data before overwriting it? I've done most of my OO programming in Java, so am not sure how much memory management C++ does on its own.
    Overwriting does not cause memory leaks. What would cause a memory leak is if delete[] is not called for the dynamic array, but if you use a std::vector, then the delete[] is done for you automatically.
    Last edited by laserlight; 07-26-2006 at 04:52 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to rotate child objects in 3D
    By Arianiv in forum Game Programming
    Replies: 11
    Last Post: 04-03-2008, 06:09 AM
  2. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  3. Question about cout an stack object?
    By joenching in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2005, 11:10 PM
  4. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 11:01 AM
  5. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 06:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21