c++ call class from on file to another

I'm trying to call my class CharacterMenu from my other file so i can start it after the user inputs 0 to start the game. But i'm getting an "error C2360: initialization of 'character' is skipped by 'case' label"

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
  #include <iostream>
#include <stdlib.h>
using namespace std;
class CharacterMenu{
public:
	void create_menu(){
		int odaberi_klas;
		string answer;
		while (true) {
			cout << "Pick your class: " << endl << endl;
			cout << "1. Warrior" << endl;
			cout << "2. Mage" << endl;
			cout << "3. Thief" << endl;
			cin >> odaberi_klas;
			switch (odaberi_klas)
			{
			case 1:
				cout << "You Picked the warrior you focuse on strenght and hp" << endl;
				cout << "Are You sure you want to pick this character?" << endl;
				cout << /*HeroMaxHP*/ "";
				cin >> answer;
				if (answer == "yes")
				{
					//route to other file
					break;
				}
				else
				{
					cout << "returning to character selection" << endl;
					system("cls");

				}
				break;
			case 2:
				cout << "You Picked the mage you focuse on intelligenc and mp" << endl;
				cout << "Are You sure you want to pick this character?" << endl;
				cout << /*HeroMaxHP*/ "";
				cin >> answer;
				if (answer == "yes")
				{
					//route to other file
		
				}
				else
				{
					cout << "returning to character selection" << endl;
					system("cls");

				}
				break;
			case 3:
				cout << "You Picked the thief you focuse on strenght and agility" << endl;
				cout << "Are You sure you want to pick this character?" << endl;
				cout << /*HeroMaxHP*/ "";
				cin >> answer;
				if (answer == "yes")
				{
					//route to other file
					
				}
				else
				{
					cout << "returning to character selection" << endl;
					system("cls");

				}
				break;
			}
			break;
		}
	}
	CharacterMenu(){
		create_menu();
	}
};

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
#include <iostream>
#include <stdlib.h>
#include "CharactarMenu.cpp"
using namespace std;
int main()
{
   
    char usr_input;
    char end_state = 'e';
    do {
        cout << "End the dragon" << endl;
        cout << "Enter the number 0 or 1 to start or end the game" << endl;
        cout << "0. Start" << endl;
        cout << "1. end" << endl;
        cin >> usr_input;
        if (isdigit(usr_input)) {
            switch (usr_input) {
            case '0':
                system("cls");
                 CharacterMenu character;
                break;
            case '1':
                exit(1);
                break;

            }
        }
        else
        {
            cout << "Wrong input" << endl<<endl;
        }
    } while (end_state != isdigit(usr_input));
}
You need to compile and link the separate files together.

  • Which compiler are you using, and
  • are you using it from the IDE or command line?

This will help us give you direct instructions on how to compile correctly (instead of generalized instructions that may confuse you).
Last edited on
Oh i forgot i'm using visual studio 2019 and yes cmd
I believe that the error refers to this part of the code:
1
2
3
4
5
6
7
8
9
switch (usr_input) {
    case '0':
        system("cls");
        CharacterMenu character;
        break;
    case '1':
        exit(1);
        break;
}


The compiler doesn't like that you can get to case 1 and miss out CharacterMenu character; in the scope of the switch.

Try:
1
2
3
4
5
6
7
8
9
10
switch (usr_input) {
    case '0':{
            system("cls");
            CharacterMenu character;
        }
        break;
    case '1':
        exit(1);
        break;
}

and see if the error goes away.

Last edited on
Yeah that was the fix. But i tought that
1
2
3
case:
 some code
break;

is the same as
1
2
3
4
case:{
 some code
}
break

Have you learnt about scope yet?

Putting code in a block {....} means that the variables created within the block are only 'visible' to the rest of the code within the block. Code outside of the block doesn't know anything about them.


Last edited on
@DumbGuy - not when you're defining variable. Either define the variable before the switch() - in which case the variable is available to all cases.

or define the variable within a block with a specific case. In this case the variable is only in scope (available) within that block of that case and not to the other case statements.
I forgot all about scopes. I didn't think that case 1 had access to my class inside case 0. But i read up about switch case and now i understand my mistake. Ty for the help
seeplus wrote:
Either define the variable before the switch()
This got me thinking...is there anything wrong with something like:
1
2
3
4
5
6
7
8
9
10
11
    int x{};
    switch (x)
    {
        int i{}; // <-- declaring a variable here
      default:
        i = 2;
        break;
      case 1: 
        i = 1;
        break
    }

VS doesn't seem to mind it, I've just never thought about it before.


oops, see later.
Last edited on
VS most certainly does mind. i is not defined!

1
2
3
4
5
6
7
8
9
10
11
12
int main() {
	switch (int x {};  x) {
		int i {}; // <-- declaring a variable here

		default:
			i = 2;
			break;
		case 1:
			i = 1;
			break;
	}
}



>C:\develop\vc\test64\Source.cpp(8,3): error C2360: initialization of 'i' is skipped by 'case' label
1>C:\develop\vc\test64\Source.cpp(3): message : see declaration of 'i'
1>C:\develop\vc\test64\Source.cpp(5,3): error C2361: initialization of 'i' is skipped by 'default' label
1>C:\develop\vc\test64\Source.cpp(3): message : see declaration of 'i'

Last edited on
My mistake, put the test code in a file that was no longer in to test project. 😳

I just thought that would be a good place to put a variable scoped to just the switch.
Outside of the scoping issue mentioned by Grey Wolf, there is really nothing wrong with the way you are using your menu. In fact, I think it is good: create your menu only when you need it and discard it once you get the information you want out of it.

To compile, make sure to list all the modules you wish to compile together on the command prompt.

  cl /EHsc /std:c++17 /Ox /W4 MyGame.cpp CharacterMenu.cpp

You can drop the /W4 if you wish, but I always recommend cranking your compiler warnings up to max and writing code that doesn't complain.

It is also possible to compile and link individual modules (.cpp) separately, but for a small program it isn't generally an issue to just do everything at once.


There are two parts to every module except the main one: a header and the implementation:
  CharacterMenu.hpp 
  CharacterMenu.cpp 

This requires you to learn to separate the class declaration from the implementation code.

CharacterMenu.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef CHARACTER_MENU_HPP
#define CHARACTER_MENU_HPP

#include "Player.hpp"  // This file (which you must write) declares things like PlayerClass, used below


class CharacterMenu {

protected:
   PlayerClass player_class;
   ...

public:
  CharacterMenu();
  // accessors to get information user chose from the menu go here, like:
  PlayerClass get_player_class() const;
  ...
};

#endif // CHARACTER_MENU_HPP 

CharacterMenu.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdlib>
#include <iostream>

using namespace std;  // I recommend you remove this from your code and use std:: when necessary

CharacterMenu::CharacterMenu() {
  // All the stuff you did in create_menu() goes here.
  // When done, the object's variables, like player_class, should all have valid values
}

PlayerClass CharacterMenu::get_player_class() const {
  return player_class;
}

Notice how you only ever #include the header file (.hpp, or .h if you are so inclined). The implementation gets compiled and linked when you invoke the cl command at the command prompt.

Hope this helps.
Last edited on
Topic archived. No new replies allowed.