Errors while calling class from other file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#pragma once
#include <iostream>
#include <stdlib.h>
#include "BattleMenu.h"
using namespace std;
class MenuTransition{
public:
    int user_input;
    void transition()
    {
        cout << "You are encoutering a new enemy" <</*EnemyName*/endl;
        cout << "Press ENTER to continue..." << endl;
        cin >> user_input;
        BattleMenu menu;
        system("cls");
    }
    MenuTransition()
    {
        transition();
    }
    ~MenuTransition() {}
};

1
2
3
 error C2065: 'BattleMenu': undeclared identifier
 error C2146: syntax error: missing ';' before identifier 'call'
 error C2065: 'menu': undeclared identifier

i'm not sure what is causing that this is a hpp file
Visual Studio 2019
Last edited on
Where/what is CharacterMenu - it's not in the code above ??
this was from a test before i changed the code mb, BattleMenu is another class in hpp file i want to "loop" from the BattleMenu class toMenuTransition back to BattleMenu.
Last edited on
use <cstdlib> if you need something from it. .h version can cause trouble in large programs.

can't tell for sure but at a guess, there is an error in #include "BattleMenu.h" that causes battlemenu type to not be resolved which makes menu unresolved.

remember when compiling multiple files, you still provide the c++ file to the compiler.
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
#pragma once
#include <iostream>
#include <string>
#include <stdlib.h>
#include "MenuTransition.cpp"
using namespace std;
class BattleMenu
{
public:
    int HeroMaxHP=100;
    int HeroMaxMP = 10;
    int EnemyMaxHP=100;
    int Attack = 15;
    int EAttack = 10;
    int user_input;
    string current_turn="Hero";
    void combatMenu()
    {
        while (true) {
            if (current_turn == "Hero") {
                cout << "Your turn!" << endl;
                cout << "Enemy HP: " << EnemyMaxHP << endl << endl << endl;
                cout << "Hero HP:" << HeroMaxHP << "      ";
                cout << "Hero MP:" << HeroMaxMP << endl;
                cout << "1. Attack" << " " << Attack << endl;
                cout << "2. Skill" << /*Skill <<*/endl;
                cout << "3. Deffend" << /*Defend<<*/endl;
                cout << "4. Item" <</*Item<<*/endl;
                cin >> user_input;
                switch (user_input) {
                case 1: {
                    EnemyMaxHP = EnemyMaxHP - Attack;
                    cout << "Enemy lost: " << EnemyMaxHP << "HP" << endl << endl;
                    system("cls");
                }
                    break;
                case 2:
                {
                    if (!no_mana()) {
                        SkillMenu();
                    }
                    else {
                        cout << "You are out of mana" << endl;
                        continue;
                    }
                   
                    cout << "Enemy lost: " << EnemyMaxHP << "HP" << endl;
                }
                    break;
                case 3:{}
                    //HeroMaxHP - (EnemyAttack - Armor) = HeroMaxHp;
                    break;
                case 4: {
                    ItemMenu();
                    system("cls");
                }
                    break;
                }
              
            }
            else if(current_turn == "Enemy")
            {
                cout << endl;
                cout << "Enemy turn " << endl;
                HeroMaxHP = HeroMaxHP - EAttack;
                cout << "You lost: " << HeroMaxHP << "HP" << endl << endl;
                break;
            }
            break;
        }
    }
   
    void SkillMenu()
    {
        system("cls");
        while (true) {
            cout << "Pick on of your skills"<<endl;
            cout << "1.Skill Name"<<endl;
            cout << "2.Skill Name"<<endl;
            cout << "3.Skill Name"<<endl;
            cin >> user_input;
            switch (user_input)
            {
            case 1:
                cout << "1.Skill_Name";
                EnemyMaxHP = EnemyMaxHP - 30;
                HeroMaxMP = HeroMaxMP - 5;
                break;
            case 2:
                cout << "2.Skill_Name";
                break;
            case 3:
                cout << "1.Skill_Name";
                break;
            }
            break;
        }
    }
    void ItemMenu()
    {
        system("cls");
        while (true) {
            cout << "Pick on of your itmes"<<endl;
            cout << "1.Health Potion"<<endl;
            cout << "2.Mana Potion"<<endl;
            cin >> user_input;
            switch (user_input)
            {
            case 1:
                cout << "1.Item_Name";
                break;
            case 2:
                cout << "2.Item_Name";
                break;
            case 3:
                cout << "1.Item_Name";
                break;
            }
            break;
        }
    }
    void change()
    {
        if (current_turn == "Hero") current_turn = "Enemy";
        else current_turn = "Hero";
    }
    void check_for_winner()
    {
        if (HeroMaxHP <= 0)
        {
            cout << "Hero lost" << endl;
            exit(0);
        }
        if (EnemyMaxHP <= 0)
        {
            cout << "Enemy lost" << endl;
            MenuTransition transition;

        }
    }
    bool no_mana() {
        if (HeroMaxMP <= 0) {

            return true;
        }
        else
            return false;
    }
    BattleMenu()
    {
        while (true)
        {
            combatMenu();
            check_for_winner();
            change();
           
        }
        
    }
    ~BattleMenu(){}
};

this might help
You're digging large holes for yourself.

> #include <stdlib.h>
Use #include <cstdlib>

> #include "MenuTransition.cpp"
No, include the .h file, not the source file.
If you include the source file, you're soon going to run into multiply declared errors.

> using namespace std;
No, do not import the namespace in a header file.
If you really need to do this, then put it in each .cpp file.

> void combatMenu()
> {
Put the implementation in BattleMenu.cpp

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines.html#S-source

Do you have any idea how "MenuTransition" and "BattleMenu" relate to one another?
https://en.wikipedia.org/wiki/Entity%E2%80%93relationship_model

A few hours of sketching out your framework of how all these things relate to one another will save you weeks of random hacking in the hope that something will eventually work.

Once you get past the student homework "single file, single edit" stage and onto needing multiple source files, you need a plan of some sort.
Hack, hope, and dump on a forum when it doesn't work is not a long term strategy.

I put out the demo code up. I did i didn't notice it.This is the header i changed the std into cstd
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
#pragma once
#include <iostream>
#include <string>
#include <cstdlib>
#include "MenuTransition.h"
class BattleMenu
{
public:
    int HeroMaxHP = 100;
    int HeroMaxMP = 10;
    int EnemyMaxHP = 100;
    int Attack = 15;
    int EAttack = 10;
    int user_input;
    string current_turn = "Hero";
    void combatMenu()
    {
        while (true) {
            if (current_turn == "Hero") {
                cout << "Your turn!" << endl;
                cout << "Enemy HP: " << EnemyMaxHP << endl << endl << endl;
                cout << "Hero HP:" << HeroMaxHP << "      ";
                cout << "Hero MP:" << HeroMaxMP << endl;
                cout << "1. Attack" << " " << Attack << endl;
                cout << "2. Skill" << /*Skill <<*/endl;
                cout << "3. Deffend" << /*Defend<<*/endl;
                cout << "4. Item" <</*Item<<*/endl;
                cin >> user_input;
                switch (user_input) {
                case 1: {
                    EnemyMaxHP = EnemyMaxHP - Attack;
                    cout << "Enemy lost: " << EnemyMaxHP << "HP" << endl << endl;
                    system("cls");
                }
                      break;
                case 2:
                {
                    if (!no_mana()) {
                        SkillMenu();
                    }
                    else {
                        cout << "You are out of mana" << endl;
                        continue;
                    }

                    cout << "Enemy lost: " << EnemyMaxHP << "HP" << endl;
                }
                break;
                case 3: {}
                      //HeroMaxHP - (EnemyAttack - Armor) = HeroMaxHp;
                      break;
                case 4: {
                    ItemMenu();
                    system("cls");
                }
                      break;
                }

            }
            else if (current_turn == "Enemy")
            {
                cout << endl;
                cout << "Enemy turn " << endl;
                HeroMaxHP = HeroMaxHP - EAttack;
                cout << "You lost: " << HeroMaxHP << "HP" << endl << endl;
                break;
            }
            break;
        }
    }

    void SkillMenu()
    {
        system("cls");
        while (true) {
            cout << "Pick on of your skills" << endl;
            cout << "1.Skill Name" << endl;
            cout << "2.Skill Name" << endl;
            cout << "3.Skill Name" << endl;
            cin >> user_input;
            switch (user_input)
            {
            case 1:
                cout << "1.Skill_Name";
                EnemyMaxHP = EnemyMaxHP - 30;
                HeroMaxMP = HeroMaxMP - 5;
                break;
            case 2:
                cout << "2.Skill_Name";
                break;
            case 3:
                cout << "1.Skill_Name";
                break;
            }
            break;
        }
    }
    void ItemMenu()
    {
        system("cls");
        while (true) {
            cout << "Pick on of your itmes" << endl;
            cout << "1.Health Potion" << endl;
            cout << "2.Mana Potion" << endl;
            cin >> user_input;
            switch (user_input)
            {
            case 1:
                cout << "1.Item_Name";
                break;
            case 2:
                cout << "2.Item_Name";
                break;
            case 3:
                cout << "1.Item_Name";
                break;
            }
            break;
        }
    }
    void change()
    {
        if (current_turn == "Hero") current_turn = "Enemy";
        else current_turn = "Hero";
    }
    void check_for_winner()
    {
        if (HeroMaxHP <= 0)
        {
            cout << "Hero lost" << endl;
            exit(0);
        }
        if (EnemyMaxHP <= 0)
        {
            cout << "Enemy lost" << endl;
            MenuTransition transition;

        }
    }
    bool no_mana() {
        if (HeroMaxMP <= 0) {

            return true;
        }
        else
            return false;
    }
    BattleMenu()
    {
        while (true)
        {
            combatMenu();
            check_for_winner();
            change();

        }

    }
    ~BattleMenu() {}
};
Last edited on
Obviously not compiled.

Just dump it here on the hope of generating some traffic.
I'm running it normally without line 14 in the MenuTransition. And i'm not sure what this comment is i'm trying to solve a problem i don't understand.
Topic archived. No new replies allowed.