Thread: using a vector outside the function

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    137

    using a vector outside the function

    i'm writting a dll that has a vector and has a function to add an instance to the vector. heres the code i have in the dll

    PHP Code:
    vector<playerplayers(0);

    export charplayerCreate(char *""double si 0)
    {
        
    string name n;
        
    player temp(namestatic_cast<int>(si));
        
        
    players.insert(players.size(), temp);
        return (
    n);

    i want to add the line players.insert(players.size(), temp); into the function, but i'm confussed how i can get the vector into the function. can anyone show me how to do it? thank you for your help.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Pass by reference just like you would with any other variable you wanted to modify in a function. If you are passing from outside the dll, then you must be careful though, since library classes might be different internally between the dll and the calling code.

    Also note that it is clearer to use push_back instead of insert if you are adding to the end of the vector.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    hmm. i don't get it though, the program calling the function won't be able to send a reference to the vector so how will i make it an argument?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Where is the vector? If the calling code cannot pass it in to the function, then the function must have access to it somehow.

    If it is a global variable (not usually good design), then it should just be able to refer to it as your code does. Otherwise, there must be some other way to access that variable. How you do that depends on where the players variable is created and what else needs to access it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM