そこで、スコアボードと 2 人のプレーヤーを持つプログラムを実装しようとしています。シングルトン パターンを使用して、2 人のプレーヤーがスコアボードを共有するようにしようとしています。ただし、プレーヤー クラスで定義されたグローバル スコアボードでメソッドを使用しようとすると、常に「実行に失敗しました」というメッセージが表示されます。
これらは私の 2 つのヘッダー ファイルです。必要に応じて、完全な実装を提供できます。
#ifndef PLAYER_H
#define PLAYER_H
#include "scoreboard.h"
#include <string>
#include <fstream>
class Player{
private:
std::ifstream file1;
std::ifstream file2;
static Scoreboard* _game;
public:
static Scoreboard* Game();
void makeMove(const char,const std::string);
};
#endif
#ifndef SCOREBOARD_H
#define SCOREBOARD_H
class Scoreboard{
private:
int aWin;
int bWin;
int LIMIT;
int curCounter;
public:
void resetWins();
void addWin(char);
void makeMove(const int, char);
void startGame(const int, const int);
int getWin(char);
int getTotal();
int getLimit();
};
#endif /* SCOREBOARD_H */
player.ccで
Scoreboard* Player::_game = 0;
Scoreboard* Player::Game(){
if (_game = 0)
{
_game = new Scoreboard;
_game->resetWins();
}
return _game;
}
makeMove メソッドとともに