How to make a function change a variable
I can't get my functions to change the value of my variables! :(
I know usually you could put in a "&" to let the function change the value, like:
void exampleFunction(int& number)
well.. you'll see, here is my problem:
//***********************************************************************
class Knap
{
public:
void fillArrays(Knap);
int weight[99];
int value[99];
int W;
}
int main
{
Knap myKnap;
myKnap.fillArrays(myKnap);
//---------------------THE PROBLEM:------------------
//after the last statement, though, all variables (myKnap.value[i], etc.) are still zero! :(
}
void Knap::fillArrays(Knap myKnap)
{
//this function puts things in myKnap.value[i], myKnap.weight[i] and myKnap.W
//in this function, however, when I do a
cout<<myKnap.value[i],
// the values are correct. So the information is getting lost somewhere...
}
//***********************************************************************
I've tried:
in class: void fillArrays(Knap);
in main: myKnap.fillArrays(myKnap);
in funct: void Knap::fillArrays(Knap myKnap){
in class: void fillArrays(Knap&);
in main: myKnap.fillArrays(myKnap);
in funct: void Knap::fillArrays(Knap& myKnap){
in class: void fillArrays(Knap*);
in main: myKnap.fillArrays(myKnap);
in funct: void Knap::fillArrays(Knap* myKnap){
but none of these have worked yet...
I'm sure this is a really silly question but thanks for helping me out!!!!!!!! ^_^
:D
Re: How to make a function change a variable
the first one doesn't change the value of myKnap.W .
i have tried the second one and fixed some deficient point in your code. It should be correct.
Code:
#include<iostream>
using namespace std;
class Knap
{
public:
void fillArrays(Knap&);
int weight[99];
int value[99];
int W;
};
int main()
{
Knap myKnap;
myKnap.fillArrays(myKnap);
//---------------------THE PROBLEM:------------------
//after the last statement, though, all variables (myKnap.value[i], etc.) are still zero!
cout<<myKnap.W<<endl;
system("PAUSE");
}
void Knap::fillArrays(Knap& myKnap)
{
//this function puts things in myKnap.value[i], myKnap.weight[i] and myKnap.W
myKnap.W=100;
//in this function, however, when I do a
cout<<myKnap.W<<endl;
// the values are correct. So the information is getting lost somewhere...
}
Output :
Re: How to make a function change a variable
Quote:
Originally Posted by
loves_oi
i have tried the second one and fixed some deficient point in your code. It should be correct.
I'm sure you have the best intentions, but posting bad solutions without any form of explanation is not helpful.
Quote:
Originally Posted by
PlutoRocks
well.. you'll see, here is my problem:
Code:
class Knap
{
public:
void fillArrays(Knap);
int weight[99];
int value[99];
int W;
}
Please use code tags and properly indent your code to keep it readable.
Note that a class definition must be closed with a semicolon, you forgot that.
What's the reason for passing a Knap to the member function fillArrays? Since this is a normal member function, it already has access to an instance of Knap. In main you have to call the function on an instance of Knap, as in
Code:
myKnap.fillArrays(...);
The instance you called the member function on is available in the member function via the this pointer. The member variables of the Knap instance are even directly accessible. That is, you can write
Code:
void Knap::fillArrays()
{
this->weight[0] = 1;
// or just
weight[0] = 1;
}
This will change the value of the member variable weight that belongs to the instance on which you called the fillArrays member function.
What happens in your code is that the Knap argument passed to the function is passed by value, which means that a copy is made and the function works on the copy. Since you only changed values in this copy, the values in your Knap instance in main are not affected.
Re: How to make a function change a variable
wow, I feel foolish now >.<
You're right!
ok, not to sound more lame but I can't tab because my tabs get ignored, I can't find the text tools and I can't use html on here, so... oh well...
Here is a, hopefully, simplified version...
class Knap
{
public:
void fillArrays();
int weight[99];
int value[99];
int W;
};
int main
{
Knap myKnap;
//should initialize weight, value and W here...
myKnap.fillArrays();
}
void Knap::fillArrays()
{
//this function puts things in value[i], weight[i] and W
}
Thanks!!
Re: How to make a function change a variable
Quote:
Originally Posted by
PlutoRocks
wow, I feel foolish now >.<
You're right!
ok, not to sound more lame but I can't tab because my tabs get ignored, I can't find the text tools and I can't use html on here, so... oh well...
Just copy-paste the code directly from your editor.
Then put [code] in front of your code and [/code] behind it.
Re: How to make a function change a variable
Quote:
Originally Posted by
D_Drmmr
Just copy-paste the code directly from your editor.
Then put [code] in front of your code and [/code] behind it.
cool, thanks! :D