Finding an element in a vector of pointers

This is a discussion on Finding an element in a vector of pointers within the C++ Programming forums, part of the General Programming Boards category; I am attempting to execute this snippet of code: Code: bool Region::ContainsAgent ( Agent *myAgent ) { if ( find(RegionalAgents.begin(), ...

  1. #1
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738

    Finding an element in a vector of pointers

    I am attempting to execute this snippet of code:

    Code:
    bool Region::ContainsAgent ( Agent *myAgent )
    {
    	if ( find(RegionalAgents.begin(), RegionalAgents.end(), myAgent) != RegionalAgents.end() ) 
                 return true;
    	else return false;
    }
    RegionalAgents is an STL vector of type Agent*. I am passing an Agent* into the function, so essentially I just want to see if that pointer is contained in the RegionalAgents vector. Not too difficult....pretty simple concept. Unfortunately gcc is not liking it.

    When I compile it on Linux with gcc 4.3, it gives me an error saying:

    Code:
    [dpru@cycling]$ make agents
    
    g++ -Wall -o agents_simulation src/agent.cpp src/belief.cpp src/group.cpp 
    src/region.cpp src/simulation.cpp src/world.cpp src/main.cpp
    
    src/region.cpp: In member function ‚bool Region::ContainsAgent(Agent*)‚:
        src/region.cpp:31: error: no matching function for call to  
          find(__gnu_cxx::__normal_iterator<Agent**, std::vector<Agent*, 
          std::allocator<Agent*> > >, __gnu_cxx::__normal_iterator<Agent**, 
          std::vector<Agent*, std::allocator<Agent*> > >, Agent*&)
    
    make: *** [agents] Error 1
    Any ideas what my error could be?
    My Website

    "Circular logic is good because it is."

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    My Hat says "#include <algorithm>" (or, as a second choice, "use std::find").

  3. #3
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    already including algorithm....and I also am doing "using namespace std;"
    My Website

    "Circular logic is good because it is."

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, then, out comes "small `compilable' example". (I mean, I made a small compilable example, but it compiled, so that doesn't help.)

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    I can't see any problem with the code posted, so you'll have to post more relevant stuff.
    However, I do make it a point to never write:
    Code:
    if (boolean_expression)
        return true;
    else
        return false;
    when one can just write
    Code:
    return boolean_expression;
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    My version
    Code:
    class Region
    {
    public:
        vector<Agent *> RegionalAgents;
        bool ContainsAgent ( Agent *myAgent );
        bool find() {} // Conflict !
    };
    
    bool Region::ContainsAgent ( Agent *myAgent )
    {
        if ( find(RegionalAgents.begin(), RegionalAgents.end(), myAgent) != RegionalAgents.end() )
            return true;
        else return false;
    }

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,206
    You're probably running afoul of the hiding rule in some way. Bin the "using namespace std;" line, and do the call as
    Code:
       return (std::find(RegionalAgents.begin(), RegionalAgents.end(), myAgent) != RegionalAgents.end() );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 06-14-2009, 01:39 PM
  2. Thread safe double linked list
    By ShwangShwing in forum C Programming
    Replies: 7
    Last Post: 06-02-2009, 10:55 AM
  3. Hey guys..need help on pointers
    By Darkozuma in forum C++ Programming
    Replies: 5
    Last Post: 07-25-2008, 02:57 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Finding greatest element of an array
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 12-10-2001, 03:30 PM

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