如何通过game_manager接口实例化Game子类并调用其重写方法?
Let's break down the issues you're facing and fix them step by step:
1. The Root Cause of Your Link Error
Your current game_manager class declares member functions but provides no implementation for them. When you try to create a game_manager object in main, the linker can't find the definitions for methods like add_team_A_goalkeeper, hence the "undefined reference" error. Worse, even if you added empty implementations, you wouldn't get the polymorphic behavior you want (calling Game's overridden methods).
2. Turn game_manager Into a Proper Abstract Interface
To make game_manager act as a true interface (enforcing that subclasses implement all its methods), convert all its member functions to pure virtual functions, and add a virtual destructor (critical for safe polymorphic memory management):
// game_manager.h #include <string> // Make sure to include this for std::string class game_manager { public: // Pure virtual functions - no implementation, must be overridden by subclasses virtual void add_team_A_goalkeeper(int stamina, int dribble, int pass, int defend) = 0; virtual void add_team_A_defender(int stamina, int dribble, int pass, int defend) = 0; virtual void add_team_A_striker(int stamina, int dribble, int pass, int defend) = 0; virtual void add_team_B_goalkeeper(int stamina, int dribble, int pass, int defend) = 0; virtual void add_team_B_defender(int stamina, int dribble, int pass, int defend) = 0; virtual void add_team_B_striker(int stamina, int dribble, int pass, int defend) = 0; virtual void play() = 0; virtual std::string get_result() = 0; // Virtual destructor - ensures subclass destructors are called when deleting via base pointer virtual ~game_manager() = default; private: };
3. Use Polymorphic Pointers/References in main
You can't instantiate an abstract class directly. Instead, use a base class pointer (or reference) to point to a Game instance. This lets you call Game's overridden methods through the game_manager interface:
#include "game_manager.h" #include <memory> // For smart pointers (optional but recommended) int main() { // Option 1: Raw pointer (remember to delete it!) game_manager* game = new Game(); // Option 2: Smart pointer (safer, auto-manages memory) // std::unique_ptr<game_manager> game = std::make_unique<Game>(); game->add_team_A_goalkeeper(100, 10, 20, 65); game->add_team_A_defender(100, 20, 60, 80); game->add_team_A_striker(100, 70, 50, 30); game->add_team_B_goalkeeper(100, 50, 40, 50); game->add_team_B_defender(100, 85, 20, 90); game->add_team_B_striker(100, 50, 20, 10); game->play(); std::cout << game->get_result(); // If using raw pointer: clean up memory delete game; return 0; }
4. Implement All Overridden Methods in Game
You must provide concrete implementations for every method you declared in Game. For example, here's how you might implement the add_team_A_goalkeeper method (assuming your GoalKeeper class has matching member variables):
// In your Game implementation file (e.g., game.cpp) #include "game_manager.h" void Game::add_team_A_goalkeeper(int stamina, int dribble, int pass, int defend) { AGoalKeeper.stamina = stamina; AGoalKeeper.dribble = dribble; AGoalKeeper.pass = pass; AGoalKeeper.defend = defend; } // Repeat this pattern for all add_* methods... void Game::play() { // Implement your game logic here, e.g.: handle_encounter(); // Set result based on Awin/Bwin if (Awin) { result = "Team A wins!"; } else if (Bwin) { result = "Team B wins!"; } else { result = "Draw!"; } } std::string Game::get_result() { return result; } void Game::handle_encounter() { // Implement your encounter logic here to determine Awin/Bwin }
Key Notes
- Pure Virtual Functions: These make
game_managerabstract, so you can't create instances of it directly—only use it as an interface for subclasses. - Virtual Destructor: Always add this to base classes used for polymorphism, otherwise deleting a subclass via a base pointer will skip the subclass destructor, causing memory leaks.
- Smart Pointers: Using
std::unique_ptrorstd::shared_ptris safer than raw pointers because they automatically manage memory, eliminating the need for manualdelete.
内容的提问来源于stack exchange,提问作者user11090047

