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
Re: passing vector object through parameters
Quote:
Originally Posted by
projectnz
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
Re: passing vector object through parameters
Edit: Nevermind, will let them work it out from Paul's post.
Re: passing vector object through parameters
Quote:
Originally Posted by
Mybowlcut
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
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
Re: passing vector object through parameters
Re: passing vector object through parameters
Quote:
Originally Posted by
laserlight
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?
Re: passing vector object through parameters
Quote:
Originally Posted by
projectnz
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
Re: passing vector object through parameters
Quote:
Originally Posted by
projectnz
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
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?
}