Thread: vector full of pointers

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    Question vector full of pointers

    If you have a vector containing pointers, how do you get another pointer to point where one of the pointers in a vector is pointing.

    If what I said is hard to understand then here's the basic idea of what i want to accomplish
    (not actual code from a program)

    Code:
    #include <vector>
    #include <anyThingElseRequired>
    using std::whateverComesFromTheStdNamespace;
    ...
    
    vector<someObject*> v;      //vector full of pointers to someObject?
    someObject *phead;            //pointer to someObject
    
    v.put_back(phead);              //no trouble here
    
    someObject *pnext;             //pointer to someObject
    
    pnext = v[0];                        //cannot convert `std::vector<someObject*,             
                                                 //std::allocator<someObject*> >' to `someObject*' in 
                                                 //assignment

    How do i get pnext to point to the same object that the pointer stored at v[0] is pointing to?

    -Thank you for your patience

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That code should work. Are you sure you used v[0] instead of v? Are you sure v is a vector<someObject*> and not a vector<vector<someObject*> >?

    Copying and pasting the actual code that does each thing you typed out would make it easier to spot the error rather than seeing typos.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    My compiler devC++ seems to think its a"std::vector<agent*, std::allocator<agent*> >"
    I'm just glanced at a vector tutorial page five minutes ago for the first time so I can't spot any error though it should be pretty obvious

    Yes I did use v[0]. I'm thinking I've messed up the function arguments heres the actual code


    Code:
    //...heres where the function vector2Agent is called
         vector <agent*> parray;
         agent2Vector(parray, phead);
    //...
    
    
    agent *vector2Agent(vector<agent*> *parray, agent *phead) //i think the problem is here
    {
          phead = parray[0];                                    //doesn't like this
          agent *pnext = phead;
          for(int x = 1; x < agent::listLength; x++)
                  {
                      pnext->setNextAgent(parray[x]); //or this
                      pnext = pnext->getNextAgent();
                  }      
          
    }
    
    
    /*actual compiler errors:
    In function `agent* vector2Agent(std::vector<agent*, std::allocator<agent*> >*, agent*)': 
    
    no matching function for call to `agent::setNextAgent(std::vector<agent*, std::allocator<agent*> >&)' 
    */
    -Thanks for your help

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Indeed, you are passing a pointer to the vector as an argument. Hence, parray[0] is a vector. From what I see, you really want to pass a reference to the vector instead. You may also want to pass a reference to agent* if you really want the effects of phead's modification to extend to the caller.
    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
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> heres where the function vector2Agent is called
    Actually, it's calling agent2Vector, but I think we see what the problem is in this particular code. The error message you posted in the first post shouldn't be happening here, though.

    BTW, vector <agent*> is kind of shorthand for the actual class that you are using. In this case, the compiler refers to the actual class as std::vector<agent*, std::allocator<agent*>, but you can pretty much ignore that and think of it as saying vector <agent*> whenever you see std::vector<agent*, std::allocator<agent*> in the error message. The allocator part is just a default parameter tha t advanced users set for memory management. Normal vector users ignore it and let the default allocator do the work.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    Quote Originally Posted by laserlight View Post
    Indeed, you are passing a pointer to the vector as an argument. Hence, parray[0] is a vector. From what I see, you really want to pass a reference to the vector instead. You may also want to pass a reference to agent* if you really want the effects of phead's modification to extend to the caller.

    That did exactly what I wanted, thank you


    Quote Originally Posted by Daved;
    Actually, it's calling agent2Vector, but I think we see what the problem is in this particular code. The error message you posted in the first post shouldn't be happening here, though.
    opps, yeah I didn't notice I copied the wrong one


    The error msg from the first post was happening in here, I forgot to copy it. That msg went away after passing the vector and pointer by reference.

    -Thank You

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM