dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: How to make a function change a variable

Hybrid View

  1. #1
    Join Date
    Apr 2011
    Posts
    3

    Unhappy 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!!!!!!!! ^_^

  2. #2
    Join Date
    Mar 2011
    Location
    Washington D.C.
    Posts
    44

    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 :
    Code:
    100
    100

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: How to make a function change a variable

    Quote Originally Posted by loves_oi View Post
    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 View Post
    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

  4. #4
    Join Date
    Apr 2011
    Posts
    3

    Resolved 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!!

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: How to make a function change a variable

    Quote Originally Posted by PlutoRocks View Post
    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

  6. #6
    Join Date
    Apr 2011
    Posts
    3

    Talking Re: How to make a function change a variable

    Quote Originally Posted by D_Drmmr View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)