Thread: Vector elements disappearing

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    12

    Vector elements disappearing

    I have a void function with a vector as an argument. The function fills the vector, but I want to use this vector as the argument in a second function. When I try to use it in the second function, the vector is empty.

    For example:
    Code:
    void Class :: Function1 (vector<double> v)
    {
    //Fill Vector
    }
    void Class :: Function2 (vector <double> v)
    {
    //Multiplies vector elements with a constant
    }
    It was all working fine, but I had to drastically alter my code and that's when this problem appeared. I've been searching for a solution using google and seen some suggestions to return the vector from the function or use pointers. I'm struggling to implement either of those ideas!

    Any help appreciated,
    Thanks a lot!

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You need to pass your vectors by reference instead of by value.
    Code:
    void Class :: Function1 (vector<double>& v)
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    12
    Dear God... I had ampersands and asterisks in every possible combination just trying to make it work.

    Thanks for that, problem solved!

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    12
    Ah, just one more thing. How does that work when it comes to calling the function?

    I've created an instance of the class and called function1 then function2, hoping to end up with a vector filled with the elements set in function2. However, I get the vector back filled with the contents set in function1.

    For example:

    Code:
    vector<double> NewVec;
    
    Class Instance;
    
    Instance.function1(NewVec);
    Instance.function2(NewVec);
    The result is as if it doesn't go through function2.

    Again, any tips are appreciated.
    Thanks again!

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    post the full code.
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    12
    Code:
    int main(int argc, char *argv[])
    {
    	vector<double> vel;
    
    	FBel Test;
    
    	Test.WSpee(vel);
    	Test.WSPro(vel);
    }
    I'm guessing the problem is with how I am calling the functions. I've tested the outputs from within the functions themselves and they are all correct. The problem only occurs when I try to call from the main.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    12
    Got it sorted, thanks again bit hub.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replace elements in vector
    By franse in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2008, 12:46 PM
  2. comparing vector elements
    By l2u in forum C++ Programming
    Replies: 5
    Last Post: 10-22-2008, 12:23 PM
  3. vector of 2d array elements?
    By jmartrican in forum C++ Programming
    Replies: 6
    Last Post: 10-02-2007, 09:05 PM
  4. deleting elements of a vector
    By strickey in forum C++ Programming
    Replies: 11
    Last Post: 02-09-2005, 11:19 AM
  5. Vector elements
    By strickey in forum C++ Programming
    Replies: 5
    Last Post: 02-04-2005, 10:30 AM