How to keep Guesser object in scope

I need to make a new Guesser object in Game, but I don't know where to put it so that all of the Game functions can see it.

GAME.CPP

#include "guesser.h"
#include "provider.h"
#include "game.h"

using namespace std;

Game::Game (int& wordLength, const char* words, Provider& prov)
{
wordSoFar = string(wordLength, FILL_CHARACTER);
numMissedGuesses = 0;
Guesser guesser (wordLength, words); // The problem: goes out of scope.
}

// Indicates whether the game is finished and, if so,
// if the guesser has won
bool Game::guesserHasWon()
{
return wordSoFar.find(FILL_CHARACTER) == string::npos;
}

// Indicates whether the game is finished and, if so,
// if the guesser has lost
bool Game::guesserHasLost()
{
return numMissedGuesses >= MAX_MISTAKE_LIMIT;
}

// Update game state to reflect a new guess
void Game::guessHasBeenMade (char guess)
{
bool isInWord;
provider.getResponseToGuess (guess, isInWord, wordSoFar, numMissedGuesses);

if (isInWord)
{
guesser.characterIsInWord (guess, wordSoFar);
}
else
{
++numMissedGuesses;
guesser.characterIsNotInWord (guess);
}
}

string Game::getWordSoFar()
{
return wordSoFar;
}

int Game::getNumMissedGuesses()
{
return numMissedGuesses;
}

Guesser Game::getGuesser()
{
return guesser;
}

Provider Game::getProvider()
{
return provider;
}
------------------------------------------------
GAME.H

#ifndef GAME_H
#define GAME_H

#include "guesser.h"
#include "provider.h"

#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>

class Provider;
class Guesser;

class Game{
public:

// A non-alphabetic character
static const char FILL_CHARACTER = '.';

// Maximum number of mistaken guesses permitted before the guesser has
// lost the game.
static const int MAX_MISTAKE_LIMIT = 10;

Game (int& wordLength, const char* words, Provider& prov);

// Indicates whether the game is finished and, if so,
// if the guesser has won
bool guesserHasWon();

// Indicates whether the game is finished and, if so,
// if the guesser has lost
bool guesserHasLost();

// Update game state to reflect a new guess
void guessHasBeenMade (char guess);

std::string getWordSoFar();

int getNumMissedGuesses();

Guesser getGuesser();

Provider getProvider();

private:

// What is known of the word being guessed. This string is of the
// correct length, with all known characters filled in and the unknown
// positions containing FILL_CHARACTER.
std::string wordSoFar;

// How many characters have been guessed that are not in the word?
int numMissedGuesses;

Provider provider;

};

#endif
---------------------------------------
GUESSER.H

#ifndef GUESSER_H
#define GUESSER_H

#include <string>
#include <vector>

class Game;

class Guesser {
public:
// Initialize the guesser for a game with the indicated wordlength,
// using words from an indicated file.
Guesser (int wordLength, const char* wordListFilename);


/**
* Scan the words that are possible solutions so far, counting, for
* each letter not already tried, the number of words with that letter.
* Guess the letter that occurs in the most words.
*/
char guessACharacter();


/**
* Following a successful guess of a letter in the word, make a pass through
* the possibleSolutions, removing all words that do not contain the
* guess character in the positions indicated in wordSoFar.
*/
void characterIsInWord (char guess, const std::string& wordSoFar);


/**
* Following a mistaken guess of a letter in the word, make a pass through
* the possibleSolutions, removing all words that contain the
* guess character.
*/
void characterIsNotInWord (char guess);


/**
* Guesser has lost the game. Look at the provider's actual word
* and gripe a bit about losing.
*/
void admitToLoss (std::string actualWord, const std::string& wordSoFar);
private:

// A collection of words that match all guesses made so far
std::vector<std::string> possibleSolutions;

// Tracks characters already guessed.
// charactersTried[c-'a'] is true if the character c
// has been guessed previously
bool charactersTried[26];

static const std::string alphabet;
};

#endif
Last edited on
You should place it in the class definition as a member variable.
Well I can't put "Guesser guesser (wordLength, words);" as a member variable because of the parameters.
I tried putting "Guesser guesser;" in Game.h and then "guesser (wordLength, words);" in Game::Game, but then I get the error no matching call to function Guesser::Guesser() at this line -> Game::Game (int& wordLength, const char* words, Provider& prov)
and the error "no matching fnction for call to Guesser (int&, const char*&)" at the line -> guesser (wordLength, words);

I'm sorry if I'm missing something really obvious. I just started using classes.
Last edited on
You need to specify the correct Guesser constructor in the ctr-init-list.
1
2
3
4
5
6
Game::Game (int& wordLength, const char* words, Provider& prov)
:	guesser(wordLength, words)
{
	wordSoFar = string(wordLength, FILL_CHARACTER);
	numMissedGuesses = 0;
}


It's a good idea to initialize the other member variables the same way.
1
2
3
4
5
6
Game::Game (int& wordLength, const char* words, Provider& prov)
:	guesser(wordLength, words),
	wordSoFar(wordLength, FILL_CHARACTER),
	numMissedGuesses(0)
{
}

Last edited on
Thanks! That helped a lot. My professor never explained that. *sigh*
Last edited on
Checked your code. There seems to be a few output errors. I've tried fixing them but I have not had any success. Just wanted to point out that there were flaws. Sorry I couldn't help.
Topic archived. No new replies allowed.