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.hpp1 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.cpp1 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.