passing by reference

First of all hello. I am making a small rag game for practice but i struggle in a certain part. As the title says, I do not know how to change the value of a variable. Below is a part of the program:

Not the main; just the function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void ss::ssf(cs cso){
    
    
    switch(cso.nc){   
            
        case(1):
            
            health = 11;
            dexterity = 6;
            heal = 3;
            damage = 10;
            mana = 2;
            
            break;

        case(2):
            
            health = 20;
            dexterity = 1;
            heal = 1;
            damage = 8;
            mana = 2;
            
            break;
}


Inside the header file I have created the above variables:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class ss{

    
public:
    ss();
    void ssf(cs cso);
    
    
    
    
    
private:
    
    friend class cs;
    int health;
    int dexterity;
    int heal;
    int damage;
    int mana;
    
    
    
};


Now the thing is that when i run the program the health etc are displayed as 0 and not as ex.11. The nc is from another class.Any help is appreciated. Thanks in advance.
Hi, I'm having some issues adjusting the bolts on my tuxedo.
This is the hammer that I'm using.


You need to provide enough to reproduce your issue.
Also, use meaningful names for your variables and classes.
Ok. Sorry for the lack of information given. So, I created 2 classes: the cs(the user selects the class) and the ss(sets the stats for the specific class selected). Now in the first class(class selection) there is a function called css(class selection function) where the user types the class that he wants (Wizard, Warrior, Rogue, Cleric) and then depending on what class the user has selected, the variable ns(number of classification) changes. I did that so I can simply use 1 instead of Wizard etc.
Now in the second class called ss(set stats) there is a function called (ssf) where the program changes the values of the different attributes depending on the class(c) selected. The 2 classes are friend with each other so they can share the values set. Now even though the values have been changed inside the ssf the real values are still the same and therefore when I try to display them they are displayed as 0. Inside the header file for the set stats class I have created the variables health, dexterity etc.
So the question is how I can keep the values set inside the set stats function? If you need any additional information I will be glad to provide it.
Thank you for your time
I can see that from the code you've posted (nice to that some context, though).
Give something that I could compile and run and obtain the wrong output that you are having. http://www.eelis.net/iso-c++/testcase.xhtml

your `ssf()' should modify the object fine, the problem may be in how you are checking that later.
Thank you for your time. Now I have another question: How can I upload the code to this site http://ideone.com ? The code is 5 different files(main,cs.cpp, cs.hpp, ss.cpp, ss.hpp) and I suppose that if I just paste the one on top of the other it will not run correctly. Even so, below there are the links for each individual file.

MAIN: http://ideone.com/wRzN8m
Class selection : http://ideone.com/XSbaeS
Class selection header file :http://ideone.com/i7uhMB
Set stats: http://ideone.com/NyLlw0
Set stats header file : http://ideone.com/sfVjij

The acronyms I use:

csf = classselectionfunction
cso = classselectionobject
cs = classselection
c = classification
nc = numberclassification
ss =setstats
ssf = setstatsfunction
sos = setstatsobject

Thank you for your time.





> The acronyms I use:
again, you should stop doing that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int main ()
{
	cs cso;
	cso.csf ();

	ss sso;
	sso.ssf (cso);

	return 0;
}

void cs::csf ()
{
	cin >> c; // c is the classification selected(Wizard, Warrior etc)
	//...
	ss sso; //this is a local variable, and uninitialized

	// printing the stats

	cout << "health = " << sso.health << endl;
	cout << "dexterity = " << sso.dexterity << endl;
	cout << "heal = " << sso.heal << endl;
	cout << "damage = " << sso.damage << endl;
	cout << "mana = " << sso.mana << endl;
}


So you are printing the state of a local variable.
You never called `.ssf()' on that object, so you'll get garbage.
Sorry for my stupidity but i do not get what is wrong. In line 16 of your reply i created an object(sso) so I can use it to access the variables health etc.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main ()
{
	cs cso;
	//cso.csf (); //expanding this
	{
		cin >> c;
		ss sso; //never initialized

		cout << "health = " << sso.health << endl;
		cout << "dexterity = " << sso.dexterity << endl;
		cout << "heal = " << sso.heal << endl;
		cout << "damage = " << sso.damage << endl;
		cout << "mana = " << sso.mana << endl;
	}

	ss sso; //different variable than the one in line 7
	sso.ssf (cso);

	return 0;
}
you are printing `foo' stats, before setting `bar'
1
2
3
4
5
cs cso;
ss foo;
cout << foo.health << '\n';
ss bar;
bar.ssf (cso);

So, thank you for your help. I thought of making it simpler and create just 2 functions inside of 1 single class. The first functions is called csf where some values are set like the number classification(nc). The thing is that those values now are displayed correctly only inside this function and when I try to use them in another function their value is 0(or nothing). I think that's because I have changed a copy of the variables and not the variables themselves.

main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include "cs.hpp"
#include <string>
using namespace std;

int main(){
    
    cs cso;
    
    cso.csf();
    
    cso.ssf();
    
    
    
    
    
    return 0;
}




cs.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include "cs.hpp"
#include <string>
using namespace std;

cs :: cs(){
    
}

void cs::csf (){
    
    cs cso;
    
    
    
    cout<<"Select Class: ";
    
    cin>>cso.c;
    
    
    
    
    
    if((cso.c =="Wizard") || (cso.c =="Warrior") ){
       
    }
    
    else{//start of else
        
        while((cso.c !="Wizard") || (cso.c !="Warrior")){//start of while
            
            cout<<"Available classes:Wizard,Warrior"<<endl;
            cout<<"Please enter class again"<<endl;
            cin>>cso.c;
            
            if((cso.c == "Wizard") || (cso.c == "Warrior")){//start of if
                cout<<"your class is: "<<cso.c<<endl;
                
                break;
            }//end of if          
        }//end of while
    }//end of else
 
    
    //start of setting numberclassification
    if (cso.c=="Wizard"){
        
        cso.nc = 1;
    }
    
    else if(cso.c=="Warrior"){
        
        cso.nc = 2;
    }
    
    //end of setting numberclassification
    
    cout<<cso.nc<<endl;//here the value set above is displayed correctly
}



void cs :: ssf(){
    
    cs cso;

    switch(cso.nc){   //start of switch
            
        case(1):
            
            cso.health = 11;
            cso.dexterity = 6;
            cso.heal = 3;
            cso.damage = 10;
            cso.mana = 2;
            
            break;
            
            
            
            
        case(2):
            
            cso.health = 20;
            cso.dexterity = 1;
            cso.heal = 1;
            cso.damage = 8;
            cso.mana = 2;
            
            break;
     
            
            
    }  //end of switch

    
    cout<<"Good..."<<endl;
    sleep(1);
    
    cout<<"Now you are a "<<cso.c<<endl; 
     //above when I try to display the c string that has 
     //been set in the csf function is displayed as nothing 
    sleep(1);
    
    cout<<cso.c<<"'s stats are the following:" <<endl;
    
    
    cout<<"health = "<<cso.health<<endl;
    cout<<"dexterity = "<<cso.dexterity<<endl;
    cout<<"heal = "<<cso.heal<<endl;
    cout<<"damage = "<<cso.damage<<endl;
    cout<<"mana = "<<cso.mana<<endl;
    
}


Happy holidays and thank you for your time.


> I think that's because I have changed a copy of the variables
Not even a copy, you are simply creating another object.
1
2
3
4
5
6
7
8
9
void cs :: ssf(){
    
    cs cso; //this is not the same variable as the one in main()[.code]

You need to understand that names are local. Variables declared in different functions have no relationship between them, it doesn't matter that they have the same name.
Otherwise it would be quite confusing.


Notice that to call a member function you need an object [code]cso.csf(); 

That object is passed as a hidden parameter, the this pointer, so you may refer to it inside the function.

So you may do
1
2
3
void cs :: ssf(){
   this->damage = 42;
}
Doing this->member_variable or simply member_variable is equivalent inside a member function.


Happy Halloween.
Last edited on
Ok. You help me a lot. If I make the variables global and write them on top of the main(), how can I then use them inside the functions of a class? Sorry for changing the question with every post.
Happy Halloween
I read some more tutorials and If I got it correctly every object creates a different set of variables and when for instance you use the object1, you manipulate a copy of the variables created. Then if you want to use the same data that you have set using the object1, you have to use the same object otherwise the data just won't have a value. So if you have the variable A, you can change it using an object1 and an object2. Then the variable A will have 2 values, depending on the object that you use to access it. Please correct me if something I did't got it right.

EDIT: 1 more thing: I read that using the private or protected section of a class is better because the public section slows down the program. Is that correct?
Last edited on
Topic archived. No new replies allowed.