error:no matching call to function Guesser::Guesser()

I was given the files hangman.cpp, guesser.cpp, guesser.h, provider.cpp, provider.h, game.cpp, and game.h. I have to change the game files (originally not in class format) so that the whole program works. My main problem is making a new Guesser object that all of the Game functions can see. Any tips on how to make the other game functions see the Guesser object would be appreciated.
Last edited on
Could you please post your full source (maybe try to format it correctly as well)? It would help us help you a lot faster.
1
2
3
4
5
6
7
8
Game::Game (int& wordLength, const char* words, Provider& prov)
{
    wordSoFar = string(wordLength, FILL_CHARACTER);
    numMissedGuesses = 0;
    provider = &prov;
    Guesser guess (wordLength, words);
    guesser = &guess;
}



guess goes out of scope when the function returns, so guesser is pointing at a place in memory it isn't safe to point at. I'm not sure why you're using pointers in Game anyway. Looks like it would be simpler to make guesser a Guesser, and provider a Provider.
Okay. I'll just show you the whole Game file. I was given the files hangman.cpp, guesser.cpp, guesser.h, provider.cpp, provider.h, game.cpp, and game.h. I had to change the game files (originally not in class format) so that the whole program works. My main problem is making a new Guesser object that all of the Game functions can see. I tried a lot of different things to just get it to run (thus the weird pointers in Game.cpp) and the provider functions seem to work fine. The guesser function guessACharacter works fine as far as I can tell. It's the guesser function characterIsInWord that makes the program crash. I'm not sure why, but it might have something to do with the getting the possible words from "words" file?

Hangman.cpp

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

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


using namespace std;

int main (int argc, char** argv)
{
Provider provider;

int wordSize = provider.initialPrompt();

Game game (wordSize, "words", provider);

bool gameFinished = false;

while (!gameFinished) {

char guess = game.getGuesser().guessACharacter();

game.guessHasBeenMade(guess);

gameFinished = game.guesserHasWon() || game.guesserHasLost();
}


if (game.guesserHasWon())
game.getProvider().providerHasLost(game.getWordSoFar());
else
{
string actualWord = game.getProvider().providerHasWon();
game.getGuesser().admitToLoss (actualWord, game.getWordSoFar());
}


return 0;
}

----------------------------------
GUESSER.CPP
using namespace std;

const std::string Guesser::alphabet = "abcdefghijklmnopqrstuvwxyz";

// Initialize the guesser for a game wit hthe indicated wordlength,
// using words from an indicated file.
Guesser::Guesser (int wordLength, const char* wordListFilename)
{
for (int i = 0; i < 26; ++i)
charactersTried[i] = false;

string word;
ifstream in ("words.txt");

while (in >> word)
{
cout << word;
if (word.size() == wordLength)
{
// word is of desired length
if (word.find_first_not_of(alphabet) == string::npos) {
// word contains only lowercse alphabetics
possibleSolutions.push_back (word);
}
}
}
in.close();

}


/**
* 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 Guesser::guessACharacter()
{
int counts[26];
for (int i = 0; i < 26; ++i)
counts[i] = 0;

// Count the number of words in which each letter can be found
for (int i = 0; i < possibleSolutions.size(); ++i)
{
string word = possibleSolutions[i];
for (char c = 'a'; c <= 'z'; ++c)
{
if (!charactersTried[c- 'a'])
{
// Character c has not been tried yet
if (word.find(c) != string::npos)
// c is in this word
++counts[c - 'a'];
}
}
}

// Find the character that occurs in the most words
char guess = ' ';
int maxSoFar = -1;
for (char c = 'a'; c <= 'z'; ++c)
{
if (counts[c - 'a'] > maxSoFar)
{
guess = c;
maxSoFar = counts[c - 'a'];
}
}


if (maxSoFar <= 0)
{
guess = 'a';
while (charactersTried[guess-'a'])
++guess;
}

charactersTried[guess-'a'] = true;
return guess;
}
/**
* 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 Guesser::characterIsInWord (char guess, const string& wordSoFar)
{
vector<string> remainingSolutions;
for (int i = 0; i < possibleSolutions.size(); ++i)
{
string wd = possibleSolutions[i];
bool OK = true;
for (int k = 0; OK && k < wordSoFar.size(); ++k)
{
if (wordSoFar[k] == guess)
{
if (wd[k] != guess)
{
OK = false;
}
}
}
if (OK)
{
//cerr << "Keeping " << wd << endl;
remainingSolutions.push_back (wd);
}
}
possibleSolutions = remainingSolutions;
}
}
----------------------------------
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
----------------------------
GAME.CPP
#include "guesser.h"
#include "provider.h"
#include "game.h"

using namespace std;

// A non-alphabetic character
//Game::FILL_CHARACTER = '.';


// 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.
//string wordSoFar;


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


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

Game::Game (int& wordLength, const char* words, Provider& prov)
{
wordSoFar = string(wordLength, FILL_CHARACTER);
numMissedGuesses = 0;
provider = &prov;
Guesser guess (wordLength, words);
guesser = &guess;
}

// 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;
Guesser& g = *guesser;
provider->getResponseToGuess (guess, isInWord, wordSoFar, numMissedGuesses);

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

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

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

Guesser Game::getGuesser()
{
Guesser& g = *guesser;
return g;
}

Provider Game::getProvider()
{
Provider p = *provider;
return p;
}

----------------------
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:

static const char FILL_CHARACTER = '.';

static const int MAX_MISTAKE_LIMIT = 10;

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

bool guesserHasWon();

bool guesserHasLost();

void guessHasBeenMade (char guess);

std::string getWordSoFar();

int getNumMissedGuesses();

Guesser getGuesser();

Provider getProvider();

private:

std::string wordSoFar;

int numMissedGuesses;

Guesser *guesser;

Provider *provider;

};

#endif

Any tips on how to make the other game functions see the Guesser object would be appreciated.
Last edited on
in your hangman.cpp why do you have provider as a parameter for your call to game if your not doing anything with it in your constructor Game::Game in game.cpp?
Why would guesser and provider be pointers in the game.h file?
Topic archived. No new replies allowed.