Problem using a vector iterator in a loop.

This is a discussion on Problem using a vector iterator in a loop. within the C++ Programming forums, part of the General Programming Boards category; The two vectors contain pointers of base class actor, which are used to point to members of two derived classes ...

  1. #1
    C++ Enthusiast M.Richard Tober's Avatar
    Join Date
    May 2011
    Location
    Georgia
    Posts
    56

    Problem using a vector iterator in a loop.

    The two vectors contain pointers of base class actor, which are used to point to members of two derived classes (player and enemy) and polymorphically call the Update function from the current Enemy, with the current player as an argument.

    I believe I have to use pointers, to allow polymorphism (though I read references could be used also, I'd love to be able to do that) since Player's Update function differs from Enemy's.

    Code:
        ///////////////
        // Game Loop //
        ///////////////
        while (Game.IsOpened())
        {
            if (UpdateClock.GetElapsedTime() > GameLoopTimeLimit)
            {
                for (vector<Actor*>::iterator p_it = Players.begin(); p_it != Players.end(); ++p_it)
                {
                    GetInput(Game, p_it);
                    for (vector<Actor*>::iterator e_it = Enemies.begin(); e_it != Enemies.end(); ++e_it)
                    {
                        *(e_it)->Update(p_it);
                    }
                }
                UpdateClock.Reset();
            }
            Render(Game, Camera, Players, Enemies);
        }
    The error I get is:
    Code:
    /storage/Projects/Recent/Refactored Game/src/main.cpp|156|error: request for member ‘Update’ in ‘* e_it.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator-> [with _Iterator = Actor**, _Container = std::vector<Actor*>, __gnu_cxx::__normal_iterator<_Iterator, _Container>::pointer = Actor**]()’, which is of non-class type ‘Actor*’|
    I may not have explained the idea well, but basically a vector of pointers to Player is looped though, and for each player, each Enemy Pointer has it's Enemy Object's Update function run with the current player as an argument.

    Any help fixing this code up would be wildly appreciated.

    Edit:
    I read through and realized I may not have made it clear what I want to do.

    I want to call the appropriate Update function, of the Enemy Object, pointed to by e_it. If anyone can help me just decipher that error code, I can work on it myself. I really don't want or expect anyone to re-write my code for me.
    Last edited by M.Richard Tober; 10-29-2011 at 09:30 AM. Reason: Clarity
    Eventually, I decided that thinking was not getting me very far and it was time to try building.
    Rob Pike, "The Text Editor sam"

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,503
    Code:
    *(e_it)->Update(p_it);
    should be

    Code:
    (*e_it)->Update(p_it);

  3. #3
    C++ Enthusiast M.Richard Tober's Avatar
    Join Date
    May 2011
    Location
    Georgia
    Posts
    56
    I gave it a go and got...

    Code:
    /storage/Projects/Recent/Refactored Game/src/main.cpp|159|error: no matching function for call to 'Actor::Update(std::vector<Actor*>::iterator&)|
    Now, reading the error message, I get that there's no match in the ACTOR class for Update, besides the virtual void Update in the actor class header. I will review some lessons on polymorphism. Thank you for your attention Elkvis!

    Here's that virtual definition from actor.h:
    Code:
    virtual void Update() {};
    Eventually, I decided that thinking was not getting me very far and it was time to try building.
    Rob Pike, "The Text Editor sam"

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Update does not take any arguments, so why do you pass an argument to it?
    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

  5. #5
    C++ Enthusiast M.Richard Tober's Avatar
    Join Date
    May 2011
    Location
    Georgia
    Posts
    56
    Go easy on me, but -
    Code:
     virtual void Update() {};
    Is from Entity::Actor (actor.h), the parent class.

    In Actor::Player (player.h) it becomes:
    Code:
    virtual void Update(Actor &Player);
    And in Actor::Enemy (enemy.h) it's:
    Code:
    virtual void Update(Actor &Player);
    So, is that my ignorance of polymorphism, that the base virtual method must match arguments with each of it's eventual instantiations in derived classes?

    Thank you so much as always for your attention! Edited for typo.
    Eventually, I decided that thinking was not getting me very far and it was time to try building.
    Rob Pike, "The Text Editor sam"

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Quote Originally Posted by M.Richard Tober
    So, is that my ignorance of polymorphism, that the base virtual method must match arguments with each of it's eventual instantiations in derived classes?
    Yes. There is this idea known as the Liskov substitution principle (PDF).
    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

  7. #7
    C++ Enthusiast M.Richard Tober's Avatar
    Join Date
    May 2011
    Location
    Georgia
    Posts
    56
    Thank you, this essay is a little above my experience, but I'm going chew over it - and try to bring it back around to my issues. Thank you for (what I consider) a vote of confidence in my ability to learn advanced topics. I am always grateful for yourself and all the mods and users here.
    Eventually, I decided that thinking was not getting me very far and it was time to try building.
    Rob Pike, "The Text Editor sam"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector and iterator problem
    By BeBu in forum C++ Programming
    Replies: 10
    Last Post: 03-11-2009, 07:38 AM
  2. Vector Iterator Help
    By (TNT) in forum C++ Programming
    Replies: 5
    Last Post: 11-04-2007, 01:53 PM
  3. stl vector + iterator operation
    By sujeet1 in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2007, 04:10 PM
  4. vector<object>::iterator Help
    By The Brain in forum C++ Programming
    Replies: 16
    Last Post: 03-26-2006, 06:22 PM
  5. vector/iterator program
    By Drake in forum Game Programming
    Replies: 3
    Last Post: 02-06-2006, 12:55 PM

Tags for this Thread


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