CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Dec 2009
    Posts
    37

    Exclamation passing vector object through parameters

    how do i get the vector array vBase2
    to pass through the parameter add(vBase2)

    add the value 2 to the vector

    and then recieve the value in main class

    so the output is:

    worked
    0
    Add
    1
    worked
    1

    // main class
    Code:
    vector<int> vBase2;
    
    int main()
    {
    	
    		cout << "Worked" << endl;
    		//vBase2.push_back(1);
    		cout << vBase2.size() << endl;
    		system("PAUSE");
    		
    		firstcl fc;
    		fc.Add(vBase2);
    
    		cout << "Worked" << endl;
    		cout << vBase2.size() << endl;
    		system("PAUSE");
    	
    
    	return 0;
    }
    //header
    Code:
    class firstcl 
    {
    public:
    	void Add(vector<int> vBase2);
    };

    // cpp
    Code:
    void firstcl::Add(vector<int> vBase2)
    {
    	
    	vBase2.push_back(2);
    
    	cout << "Add" << endl;
    	cout << vBase2.size() << endl;
    	system("PAUSE");
    }
    output is:

    worked
    0
    Add
    1
    worked
    0

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: passing vector object through parameters

    Quote Originally Posted by projectnz View Post
    how do i get the vector array vBase2
    to pass through the parameter add(vBase2)
    I'll let you answer your own question.

    Your problem is no different than this:
    Code:
    #include <iostream>
    
    using namespace std;
    
    void foo(int x)
    {
       x++;
    }
    
    int main()
    {
       int x = 0;
       cout << x <<"\n";
       foo(x);
       cout << x <<"\n";  //  Why is x still 0 and not 1?
    }
    Why isn't x equal to 1 in the second cout? The answer to that question is the same reason why the vector doesn't change.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: passing vector object through parameters

    Edit: Nevermind, will let them work it out from Paul's post.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: passing vector object through parameters

    Quote Originally Posted by Mybowlcut View Post
    Edit: Nevermind, will let them work it out from Paul's post.
    Yep. Having the OP answer the simple question will introduce the basic C++ concepts of passing parameters, and that vector is not special -- the same issue happens with just a plain old int.

    Regards,

    Paul Mckenzie

  5. #5
    Join Date
    Dec 2009
    Posts
    37

    Re: passing vector object through parameters

    I see x is out of scope from the x defined in main

    so just wondering how could this be achieved?
    Would I have to use the new operator somehow?

    Thanks for answers

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: passing vector object through parameters

    Hint: pass by reference
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Dec 2009
    Posts
    37

    Smile Re: passing vector object through parameters

    Quote Originally Posted by laserlight View Post
    Hint: pass by reference
    Do you mean reference the class that you want to pass the value back too?

    Is this multi inheritance. I'm used to c# etc where you only pass 1 way

    how come if you have a class with get set methods inside it. Then pass it through parameters
    you able to call set method and it should set the original class object

    isn't vector a sort of class object?

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: passing vector object through parameters

    Quote Originally Posted by projectnz View Post
    Do you mean reference the class that you want to pass the value back too?
    Time to get a C++ book, and not guess what "pass by reference" means. C# is not C++, and if you're trying to write C++ code using C# as a model -- well it is not a good idea.

    As my example shows, your problem has nothing to do with vector. It has everything to do with knowing how C++ works and how parameters are passed. These are basic fundamentals in C++. C++ has two ways to pass values -- by value and by reference. You are passing the vector (and I am passing the int in my example) by value. What does your C++ book say what happens when you pass by value? What does your C++ book say when you pass by reference?

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: passing vector object through parameters

    Quote Originally Posted by projectnz View Post
    I see x is out of scope from the x defined in main
    That is not the problem. The "x" in foo is a dummy argument to the function. It doesn't matter if I call it "x", "yy", or "Joe", the results are the same.
    Code:
    #include <iostream>
    
    using namespace std;
    
    void foo(int yy)
    {
       yy++;
    }
    
    int main()
    {
       int x = 0;
       cout << x <<"\n";
       foo(x);
       cout << x <<"\n";  //  Why is x still 0 and not 1?
    }
    You are passing the x by value. Now, what does pass by-value mean? As I stated in my last post, this has nothing to do with vectors and classes.

    Passing by-value makes a temporary copy of the value, and the function works on this temporary copy. When the function returns, that temporary copy is destroyed. This basic fundamental should have been covered in whatever C++ book you're using. Passing by reference actually works with the variable you're passing, not a temporary copy.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 5th, 2010 at 08:20 AM.

  10. #10
    Join Date
    Dec 2009
    Posts
    37

    Re: passing vector object through parameters

    ohh ok i get it now thanks for your help

    Code:
    #include <iostream>
    
    using namespace std;
    
    void foo(int &x)
    {
       x++;
    }
    
    int main()
    {
       int x = 0;
       cout << x <<"\n";
       foo(x);
       cout << x <<"\n";  //  Why is x still 0 and not 1?
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured