Calling class function from another class

Pages: 12
I have my program and instead of dinking around witht he save thing im going to try to make a save class but it cant find the variables to save so i was wondering how to call a class function from another class, i tried but dont think im doing it right since i got errors

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
#include <iostream>
#include <string>
#include <ctime>
#include <random>
#include <fstream>

using namespace std;

class CLASS
{
    public:
        void Player();
        void TRex();
        void Upgrades();
        void Help();
        void Stats();
        void Load();
        CLASS()
        {
            PHealth = 100;
            TRHealth = 100;
            XP = 0;
            NumDef = 0;
            PKill = 0;
        }
    private:
        int PHealth; //Player Health
        int TRHealth; //T-Rex Health
        int XP; //Experience points
        int NumDef; //Number of opponents defeated
        int PKill; //Number of times player was killed
};


class SAVEG
{
    public:
        void Save();
        SAVEG()
        {
            CLASS();

            file.open("stats.txt");
            file << XP << endl;
            file << NumDef << endl;
            file << PKill << endl;
            file.close();
        }
    private:
        ofstream file;
};



C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp||In constructor 'SAVEG::SAVEG()':|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp|42|error: invalid use of 'class CLASS'|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp|46|error: 'XP' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp|47|error: 'NumDef' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp|48|error: 'PKill' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp|86|error: 'class CLASS' has no member named 'Save'|
||=== Build finished: 5 errors, 0 warnings ===|
Last edited on
It seems to me that SAVEG::SAVEG() should be made a member function of CLASS and renamed to Save(). What you're doing now makes no sense.
1
2
3
4
5
6
7
8
9
10
11
12
class CLASS
{
    //...
    void Save()
    {
        std::ofstream file("stats.txt");
        file << XP << endl;
        file << NumDef << endl;
        file << PKill << endl;
    }
    //...
}
Last edited on
Like this?

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
#include <iostream>
#include <string>
#include <ctime>
#include <random>
#include <fstream>

using namespace std;

class CLASS
{
    public:
        void Player();
        void TRex();
        void Upgrades();
        void Help();
        void Stats();
        void Load();
        void Save();
        CLASS()
        {
            PHealth = 100;
            TRHealth = 100;
            XP = 0;
            NumDef = 0;
            PKill = 0;
        }
        void Save()
        {
            file.open("stats.txt");
            file << XP << endl;
            file << NumDef << endl;
            file << PKill << endl;
            file.close();
        }
    private:
        int PHealth; //Player Health
        int TRHealth; //T-Rex Health
        int XP; //Experience points
        int NumDef; //Number of opponents defeated
        int PKill; //Number of times player was killed
        ofstream file;
};



i still get some errors


C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp|27|error: 'void CLASS::Save()' cannot be overloaded|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp|18|error: with 'void CLASS::Save()'|
||=== Build finished: 2 errors, 0 warnings ===|


I changed it to this and it works:

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
#include <iostream>
#include <string>
#include <ctime>
#include <random>
#include <fstream>

using namespace std;

class CLASS
{
    public:
        void Player();
        void TRex();
        void Upgrades();
        void Help();
        void Stats();
        void Load();
        void Save();
        CLASS()
        {
            PHealth = 100;
            TRHealth = 100;
            XP = 0;
            NumDef = 0;
            PKill = 0;
        }
        void SAVEG()
        {
            file.open("stats.txt");
            file << XP << endl;
            file << NumDef << endl;
            file << PKill << endl;
            file.close();
        }
    private:
        int PHealth; //Player Health
        int TRHealth; //T-Rex Health
        int XP; //Experience points
        int NumDef; //Number of opponents defeated
        int PKill; //Number of times player was killed
        ofstream file;
};




is that right?
Last edited on
The reason why you got that error is because you declared and defined the Save() method inside the class declaration.

You need to either declare it and define it outside the class declaration or just declare and define it inside the class declaration.

Basically you need to make up your mind. Either define ALL methods inside the class declaration or define all of them outside of it. The former is a shortcut but is messy and defeats the purpose of having an interface.

*I only define methods inside the class declaration for example purposes.

P.S.- What kind of game are you trying to develop? You might want to spend some more time coming up with a better design. You could create a Save class but it would need access to all the necessary data.
Last edited on
Well i got it working but i cant seem to get the save working, i have been trying a ton of different things but cant find a solution.

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <iostream>
#include <string>
#include <ctime>
#include <random>
#include <fstream>

using namespace std;

class CLASS
{
    public:
        void Player();
        void TRex();
        void Upgrades();
        void Help();
        void Stats();
        CLASS()
        {
            PHealth = 100;
            TRHealth = 100;
            XP = 0;
            NumDef = 0;
            PKill = 0;
        }
        int SAVEG()
        {
            ofstream file("stats.txt");
            file << XP << endl;
            file << NumDef << endl;
            file << PKill << endl;

            cout << "File Saved" << endl;
        }
        void LOADG()
        {
            ifstream file("stats.txt");
            file >> XP;
            file >> PKill;
            file >> NumDef;
        }
    private:
        int PHealth; //Player Health
        int TRHealth; //T-Rex Health
        int XP; //Experience points
        int NumDef; //Number of opponents defeated
        int PKill; //Number of times player was killed
};


int main()
{
    CLASS CO;

    string choice;

    cout << "\n";
    cout << "DINOSAUR ARENA" << endl;
    cout << "\n";
    cout << "Type in the number next to the choice" << endl;
    cout << "\n";
    cout << "1) Enter Game" << endl;
    cout << "2) Help" << endl;
    cout << "3) View Stats" << endl;
    cout << "4) Save game progress" << endl;
    cin >> choice;

    if(choice == "1")
    {
        CO.Player();
    }
    else if(choice == "2")
    {
        CO.Help();
    }
    else if(choice == "3")
    {
        CO.Stats();
    }
    else if(choice == "4")
    {
        CO.SAVEG();
    }
    else
    {
        main();
    }
}


void CLASS::Help()
{
    cout << "HELP SECTION" << endl;
    cout << "\n";
    cout << "1) " << endl;
}


void CLASS::Stats()
{
    string choice;

    cout << "\n";
    cout << "STATS" << endl;
    cout << "\n";
    cout << "Type in: back to go back to main menu." << endl;
    cout << "\n";
    cout << "Your total XP: " << XP << endl;
    cout << "Times you were killed: " << PKill << endl;
    cout << "Number of dinosaurs defeated: " << NumDef << endl;
    cin >> choice;

    if(choice == "Back" || choice == "back")
    {
        main();
    }
}

void CLASS::Player()
{
    string choice;

    cout << "\n";
    cout << "\n";
    cout << "What weapon will you use?" << endl;
    cout << "\n";
    cout << "1) Shotgun = 4 Damage" << endl;
    cout << "2) Pistol = 6 Damage" << endl;
    cout << "3) Knife = 5 Damage" << endl;
    cout << "\n";

    while(true)
    {
        cin >> choice;

        if(TRHealth <= 0)
        {
            cout << "You defeated the T-Rex!" << endl;
            XP += 30;
            NumDef += 1;
            cout << "XP gained: " << XP << endl;
            cout << "\n";
            cout << "\n";
            TRHealth = 100;
            main();
            break;
        }

        if(PHealth <= 0)
        {
            cout << "You were killed!" << endl;
            cout << "XP gained: 0" << endl;
            cout << "\n";
            cout << "\n";
            PKill += 1;
            PHealth = 100;
            main();
            break;
        }

        if(choice == "1")
        {
            cout << "\n";
            cout << "You used the shotgun!" << endl;
            cout << "Your health: " << PHealth << endl;
            cout << "\n";
            TRHealth -= 4;
            TRex();
        }
        else if(choice == "2")
        {
            cout << "\n";
            cout << "You used the pistol!" << endl;
            cout << "Your health: " << PHealth << endl;
            cout << "\n";
            TRHealth -= 6;
            TRex();
        }
        else if(choice == "3")
        {
            cout << "\n";
            cout << "You used the knife!" << endl;
            cout << "Your health: " << PHealth << endl;
            cout << "\n";
            TRHealth -= 5;
            TRex();
        }
    }
}


void CLASS::TRex()
{
    time_t T;
    time(&T);
    srand(T);

    int Time;

    Time = rand() % 4;

        switch(Time)
        {
            case 0:
                cout << "\n";
                cout << "T-Rex used Bite! = -6 Health" << endl;
                PHealth -= 6;
                cout << "T-rex Health: " << TRHealth << endl;
                cout << "\n";
                Player();
                break;

            case 1:
                cout << "\n";
                cout << "T-Rex used Chomp! = -5 Health" << endl;
                PHealth -= 5;
                cout << "T-rex Health: " << TRHealth << endl;
                cout << "\n";
                Player();
                break;

            case 2:
                cout << "\n";
                cout << "T-Rex used Stomp! = -4 Health" << endl;
                PHealth -= 4;
                cout << "T-rex Health: " << TRHealth << endl;
                cout << "\n";
                Player();
                break;
            default:
                cout << "\n";
                cout << "Trex's attack missed!" << endl;
                PHealth -= 0;
                cout << "T-rex Health: " << TRHealth << endl;
                cout << "\n";
                Player();
                break;
        }
}



This normally wouldnt be a problem but my program is big and its a bit disorentating. sorry i keep asking about it but i guess im just nbot seeing it, i looked up stuff about classes but it doesnt help all that much.
Last edited on
First off, use a while loop instead of calling your main( ) function over and over.
In answer to your question im making a simple battle text game where you fight a t-rex.
The problem im having isd that i give 30 points to the player but its not adding it to the variable in CLASS, i tested everything and thats the only problem im having, also how would putting my calls to main in a while loop help? im calling it in a bunch of different locations, putting it in a loop wont allow me to go back.
It's just bad practice to explicitly call main() inside main. Who told you to do that? Your main() should only be called once, when the program executes.

I have no idea i just did it myself because i didnt know of any other way of going back to the program. Well i do know of one, which would be to create another function and put all my stuff in main, in that function, then tell main to go to that function when it starts. but anyways my problem writing to the variables in CLASS is what i really need help with, i figured out how to get them to write to the file, its just when you defeat the Trex and et points it doesnt change the variables in CLASS
bump
bump
The reason you're having issues is just like everyone else has pointed out to you, you keep calling main. Now let me explain why this is bad. First, calling main is a big no no in C++, period. Second off, calling main will either erase your current class, it's as if your program never ran, or it will create another class object, which will no longer store the correct information. Why do you feel you need to call main? You know classes, you should know how to do loops, and especially since I seem them there, there is no reason why you're not using loops instead.

People have more than likely stopped responding to you since you were told several times what your issue is and you refused to correct it.
closed account (o3hC5Di1)
Hi there,

Please be a bit patient - there's other trying to get help too.

Here are some things to try:

- Close the file after loading, perhaps writing to a file open for reading is not possible.
- use ofstream.is_open() to verify whtether the file was opened or not.
- Set a breakpoint at the save function and see what the debugger has to say.

On a sidenote, it's best to name your classes relevant to its representation, for example: Class car{}; Car racecar; If you would use more than one class in your program using "class" will get very confusing.

Hope that helps.
All the best,
NwN
I did fix my main() issue here is my current code

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <iostream>
#include <string>
#include <ctime>
#include <random>
#include <fstream>

using namespace std;

class CLASS
{
    public:
        void Player();
        void TRex();
        void Upgrades();
        void Help();
        void Stats();
        CLASS()
        {
            PHealth = 100;
            TRHealth = 100;
            XP = 10;
            NumDef = 1;
            PKill = 3;
        }
        void SAVEG()
        {
            CLASS();

            ofstream file("stats.txt");
            file << XP << endl;
            file << NumDef << endl;
            file << PKill << endl;
        }
        void LOADG()
        {
            ifstream file("stats.txt");
            file >> XP;
            file >> PKill;
            file >> NumDef;
        }
    private:
        int PHealth; //Player Health
        int TRHealth; //T-Rex Health
        int XP; //Experience points
        int NumDef; //Number of opponents defeated
        int PKill; //Number of times player was killed
};

void MAINPROGRAM();

int main()
{
    MAINPROGRAM();
}

void MAINPROGRAM()
{
    CLASS CO;

    string choice;

    cout << "\n";
    cout << "DINOSAUR ARENA" << endl;
    cout << "\n";
    cout << "Type in the number next to the choice" << endl;
    cout << "\n";
    cout << "1) Enter Game" << endl;
    cout << "2) Help" << endl;
    cout << "3) View Stats" << endl;
    cout << "4) Save game progress" << endl;
    cin >> choice;

    if(choice == "1")
    {
        CO.Player();
    }
    else if(choice == "2")
    {
        CO.Help();
    }
    else if(choice == "3")
    {
        CO.Stats();
    }
    else if(choice == "4")
    {
        CO.SAVEG();
    }
    else
    {
        MAINPROGRAM();
    }
}

void CLASS::Help()
{
    cout << "HELP SECTION" << endl;
    cout << "\n";
    cout << "1) " << endl;
}


void CLASS::Stats()
{
    string choice;

    CLASS();

    cout << "\n";
    cout << "STATS" << endl;
    cout << "\n";
    cout << "Type in: back to go back to main menu." << endl;
    cout << "\n";
    cout << "Your total XP: " << XP << endl;
    cout << "Times you were killed: " << PKill << endl;
    cout << "Number of dinosaurs defeated: " << NumDef << endl;
    cin >> choice;

    if(choice == "Back" || choice == "back")
    {
        MAINPROGRAM();
    }
}

void CLASS::Player()
{
    string choice;

    cout << "\n";
    cout << "\n";
    cout << "What weapon will you use?" << endl;
    cout << "\n";
    cout << "1) Shotgun = 4 Damage" << endl;
    cout << "2) Pistol = 6 Damage" << endl;
    cout << "3) Knife = 5 Damage" << endl;
    cout << "\n";

    while(true)
    {
        cin >> choice;

        if(TRHealth <= 0)
        {
            cout << "You defeated the T-Rex!" << endl;
            XP += 30;
            NumDef += 1;
            cout << "XP gained: " << XP << endl;
            cout << "\n";
            cout << "\n";
            TRHealth = 100;
            MAINPROGRAM();
            break;
        }

        if(PHealth <= 0)
        {
            cout << "You were killed!" << endl;
            cout << "XP gained: 0" << endl;
            cout << "\n";
            cout << "\n";
            PKill += 1;
            PHealth = 100;
            MAINPROGRAM();
            break;
        }

        if(choice == "1")
        {
            cout << "\n";
            cout << "You used the shotgun!" << endl;
            cout << "Your health: " << PHealth << endl;
            cout << "\n";
            TRHealth -= 4;
            TRex();
        }
        else if(choice == "2")
        {
            cout << "\n";
            cout << "You used the pistol!" << endl;
            cout << "Your health: " << PHealth << endl;
            cout << "\n";
            TRHealth -= 6;
            TRex();
        }
        else if(choice == "3")
        {
            cout << "\n";
            cout << "You used the knife!" << endl;
            cout << "Your health: " << PHealth << endl;
            cout << "\n";
            TRHealth -= 5;
            TRex();
        }
    }
}


void CLASS::TRex()
{
    time_t T;
    time(&T);
    srand(T);

    int Time;

    Time = rand() % 4;

        switch(Time)
        {
            case 0:
                cout << "\n";
                cout << "T-Rex used Bite! = -6 Health" << endl;
                PHealth -= 6;
                cout << "T-rex Health: " << TRHealth << endl;
                cout << "\n";
                Player();
                break;

            case 1:
                cout << "\n";
                cout << "T-Rex used Chomp! = -5 Health" << endl;
                PHealth -= 5;
                cout << "T-rex Health: " << TRHealth << endl;
                cout << "\n";
                Player();
                break;

            case 2:
                cout << "\n";
                cout << "T-Rex used Stomp! = -4 Health" << endl;
                PHealth -= 4;
                cout << "T-rex Health: " << TRHealth << endl;
                cout << "\n";
                Player();
                break;
            default:
                cout << "\n";
                cout << "Trex's attack missed!" << endl;
                PHealth -= 0;
                cout << "T-rex Health: " << TRHealth << endl;
                cout << "\n";
                Player();
                break;
        }
}
That's not really fixing it. You're no longer calling main, but you're running your code over and over agan, deleting anything you've done. I believe you need to rethink your code and stop trying to use recursion. That's not the answer here.

Edit: Currently, your program is doing this:
Call MAINPROGRAM();
Create CLASS Object CO;
Create string variable choice;
Display:
"
DINOSAUR ARENA

Type in the number next to the choice

1) Enter Game
2) Help
3) View Stats
4) Save game progress
"
Enter user input to choice;
If user enter's 1, Call CO function Player;
Create string variable choice;
Display:
"


What weapon will you use?

1) Shotgun = 4 Damage
2) Pistol = 6 Damage
3) Knife = 5 Damage
"
You call an infinite loop;
Enter user input to choice;
TRHealth is 100, so if statement is ignored;
PHealth is 100 so if statement is ignored;
If user entered 1, choice == "1" is true;
Display:
"
You used the shotgun!
Your health: 100

"
TRHealth = 96;
Call TRex();
Skip several calls;
Enter user input to choice;
TRHealth is 5, so if statement is ignored;
PHealth is -3, PHealth <= 0 is true;
Display:
"You were killed!
XP gained: 0


"
PKill = 1;
PHealth = 1;
Call MAINPROGRAM();
   Create CLASS Object CO;
   Create string variable choice;
   Display:
"
DINOSAUR ARENA

Type in the number next to the choice

1) Enter Game
2) Help
3) View Stats
4) Save game progress
"
   Enter user input to choice;
   If user enter's 4, Call CO function SAVEG();
   Construct new CLASS();
   Open stats.txt to read from;
   Save information as local XP, NumDef, and PKill;
   MAINPROGRAM Deletes new CO Object;
   MAINPROGARM Terminates;
Infinite loop is still going on;
Enter user input to choice;
TRHealth is 5, so if statement is ignored;
PHealth is 100 so if statement is ignored;
If user entered 1, choice == "1" is true;
Display:
"
You used the shotgun!
Your health: 100

"
And so on.


As you can see, your program makes very little sense, and I'm not spending the next several hours just typing recursion over and over again. You need to remove the infinite loops from your programs, you also need to stop calling the functions over and over again. Like I said, you need to redesign your program and try it all over again.
Last edited on
Ok well give me an example of how its supposed to look, cause if i re-do the program it will end up being pretty much the same thing.
I won't write your entire program for you tonight, but I suggest writing down on a piece of paper exactly what you want your complete program to do. I'm not sure if you noticed it, but there is a lot of lines in there what don't need to be. You're also somewhat abusing the idea of a class. Anyways, don't even look at this code, walk away from it, and design from scratch. If you're still having issues after I get out of work tomorrow night and no one else has helped you, I'll show you how to do it.
Ok i'll work on it and try to come up with a better solution, thank you.
Ok i tried to re write it and it ended up looking very similar, i honestly dont know what to do, ive never been taught any different, actually ive never been taught how to structure programs at all. if that is even a teachable thing.
Pages: 12