Thread: Problem using a vector iterator in a loop.

  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
    3,445
    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
    28,413
    Update does not take any arguments, so why do you pass an argument to it?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 02: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, 07:22 PM
  5. vector/iterator program
    By Drake in forum Game Programming
    Replies: 3
    Last Post: 02-06-2006, 01:55 PM

Tags for this Thread