-
April 4th, 2011, 09:33 PM
#1
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!!!!!!!! ^_^
-
April 5th, 2011, 02:48 AM
#2
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 :
-
April 5th, 2011, 04:45 AM
#3
Re: How to make a function change a variable
 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.
 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.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
April 5th, 2011, 09:43 PM
#4
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!!
-
April 6th, 2011, 03:30 AM
#5
Re: How to make a function change a variable
 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.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
April 6th, 2011, 11:35 AM
#6
Re: How to make a function change a variable
 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!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
On-Demand Webinars (sponsored)
|