Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从另一个源文件声明函数;没有要调用的匹配函数";_C++ - Fatal编程技术网

C++ 从另一个源文件声明函数;没有要调用的匹配函数";

C++ 从另一个源文件声明函数;没有要调用的匹配函数";,c++,C++,我正在编写一个源文件和头文件,该文件由一个程序与其他一些文件一起实现,我只需要编辑一个源文件和头文件(game.h和game.cpp),就可以使代码正常工作。我试图从另一个源/头文件调用另一个函数,但它不能正常工作。这是我的密码 game.cpp #include "game.h" #include "guesser.h" #include "provider.h" using namespace std; const char FILL_CHARACTER = '.'; string wo

我正在编写一个源文件和头文件,该文件由一个程序与其他一些文件一起实现,我只需要编辑一个源文件和头文件(game.h和game.cpp),就可以使代码正常工作。我试图从另一个源/头文件调用另一个函数,但它不能正常工作。这是我的密码

game.cpp

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

using namespace std;

const char FILL_CHARACTER = '.';
string wordSoFar;
int numMissedGuesses;
const int MAX_MISTAKE_LIMIT = 10;

void Game::setUpGame (int wordLength)
{
    wordSoFar = string(wordLength, FILL_CHARACTER);
    numMissedGuesses = 0;
}


bool Game::guesserHasWon()
{
    return wordSoFar.find(FILL_CHARACTER) == string::npos;
}

bool Game::guesserHasLost()
{
    return numMissedGuesses >= MAX_MISTAKE_LIMIT;
}


void Game::guessHasBeenMade (char guess)
{
    bool isInWord;
    Provider::getResponseToGuess(guess, isInWord, wordSoFar);
    if (isInWord)
    {
        characterIsInWord (guess, wordSoFar);
    }
    else
    {
        ++numMissedGuesses;
        characterIsNotInWord (guess);
    }
}
#include "provider.h"
#include "game.h"

#include <iostream>


using namespace std;


int Provider::initialPrompt ()
{
  cout << "Let's play Hangman!\n\n"
       << "Please think of a word from 4-9 characters long. The word\n"
       << "should not be a proper name (something that you would normally\n"
       << "capitalize, nor should it contain any punctuation characters.\n"
       << "\n\nOK, got a word?\n" << endl;
  int len = 1;
  while (len < 4 || len > 9)
    {
      cout << "How long is your word? " << flush;
      cin >> len;
      if (len < 4 || len > 9)
    {
      cout << "Please choose a word between 4 and 9 characters long."
           << endl;
    }
    }
  return len;
}



bool getYesNoResponse()
{
  string response;
  getline (cin, response);
  while (response.size() == 0 || 
     (response[0] != 'y' && response[0] != 'Y'
      && response[0] != 'n' && response[0] != 'N'))
    {
      if (response.size() > 0)
    cout << "Please respond 'yes' or 'no'. " << flush;
      getline (cin, response);
    }
  return response[0] == 'y' || response[0] == 'Y';  
}


void Provider::getResponseToGuess (char guess, bool& isInWord,
                   std::string& wordSoFar,
                   int numMissedGuesses)
{
  cout << "I have missed " << numMissedGuesses << " guesses ("
       << Game::MAX_MISTAKE_LIMIT - numMissedGuesses << " misses left)"
       << endl; 
  cout << "\n" << wordSoFar << "\n";
  for (int i = 1; i <= wordSoFar.size(); ++i)
    cout << i;
  cout << "\n\nDoes your word contain the letter '" 
       << guess << "'? (y/n) " << flush;
  isInWord = getYesNoResponse();
  if (isInWord) {
      string response;
      bool done = false;
      string newWord;
      while (!done)
    {
      cout << "Enter all of the character positions (1-"
           << wordSoFar.size() << ") in which the letter '"
           << guess <<  "' appears: " << flush;
      getline (cin, response);
      bool digitsFound = false;
      newWord = wordSoFar;
      for (int i = 0; i < response.size(); ++i)
        {
          char d = response[i];
          if (d >= '1' && d <= '0' + wordSoFar.size())
        {
          int k = d - '1';
          if (wordSoFar[k] == Game::FILL_CHARACTER)
            {
              newWord[k] = guess;
              digitsFound = true;
            }
        }
        }
      if (digitsFound)
        {
          cout << "Like this: " << newWord << "? (y/n) " << flush;
          bool yn = getYesNoResponse();
          if (yn)
        {
          wordSoFar = newWord;
          done = true;
        }
        }
    }
  }
}



/**
 * Announce that the provider has won the game, and get the
 * provider's actual word.
 */
std::string Provider::providerHasWon ()
{
  cout << "Congratulations, you have won." << endl;
  cout << "\nOut of curiosity, what was your word? " << flush;

  string answer;
  getline (cin, answer);
  return answer;
}


/**
 * Announce that the guesser has won the game, and get the
 * provider's actual word.
 */
void Provider::providerHasLost (string wordSoFar)
{
  cout << wordSoFar
       << "\n\nI have won!\nThanks for playing" << endl;
}
#include "guesser.h"
#include "game.h"

#include <iostream>
#include <fstream>


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 (wordListFilename);
  while (in >> 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;

}


/**
 * 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 Guesser::characterIsNotInWord (char guess)
{
  vector<string> remainingSolutions;
  for (int i = 0; i < possibleSolutions.size(); ++i)
    {
      string wd = possibleSolutions[i];
      if (wd.find(guess) == string::npos)
    {
      remainingSolutions.push_back (wd);
    }
    }
  possibleSolutions = remainingSolutions;
}


/**
 * Guesser has lost the game. Look at the provider's actual word
 * and gripe a bit about losing.
 */
void Guesser::admitToLoss (std::string actualWord, const string& wordSoFar)
{
  bool match = actualWord.size() == wordSoFar.size();
  for (int i = 0; match && i < actualWord.size(); ++i)
    {
      match = wordSoFar[i] == Game::FILL_CHARACTER || 
    wordSoFar[i] == actualWord[i];
    }
  if (!match)
    {
      cout << "Ummm...your word '" << actualWord
       << "' does not match the patterh '"
       << wordSoFar <<"'.\nDid you make a mistake somewhere?"
       << endl;
    }
  else
    {
      for (int i = 0; match && i < actualWord.size(); ++i)
    {
      if (wordSoFar[i] == Game::FILL_CHARACTER
          && charactersTried[actualWord[i]-'a'])
        {
          cout << "Did you forget to mention the '"
           << actualWord[i]
           << "' in position " << i+1 << "?"
           << endl;
          return;
        }
    }

      for (int i = 0; (!match) && i < possibleSolutions.size(); ++i)
    match = (actualWord == possibleSolutions[i]);
      match = match && (possibleSolutions.size() > 0);
      if (match)
    {
      cout << "OK, I might have guessed that eventually." << endl;
    }
      else
    {
      cout << "Interesting, I don't know that word. Are you sure you\n"
           << "spelled it correctly?." << endl;
    }

    }
}
game.h

#ifndef GAME_H
#define GAME_H

#include <string>
class Provider;
class Guesser;
class Game{

    const char FILL_CHARACTER;

    std::string wordSoFar;

    int numMissedGuesses;

    const int MAX_MISTAKE_LIMIT;

    void setUpGame (int wordLength);

    bool guesserHasWon();

    bool guesserHasLost();

    void guessHasBeenMade (char guess);

};
#endif
#ifndef PROVIDER_H
#define PROVIDER_H

#include <string>


class Provider {
public:

    int initialPrompt ();
    void getResponseToGuess (char guess, bool& isInWord,
           std::string& wordSoFar,
           int numMissedGuesses);
     std::string providerHasWon ();



void providerHasLost (std::string wordSoFar);

private:
};
#endif
#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
本来只是

getResponseToGuess(guess, isInWord, wordSoFar);
但我试过几种变体。我尝试了
Provider::Provider
Provider.getReponseToGuess
,以及其他一些方法,但似乎都没有效果。有人能帮我吗?我只是想从提供程序文件调用函数

编辑:我在从guesser文件调用函数时遇到了同样的问题,我尝试了使用与provider相同的方法,但它给出了错误“没有匹配的函数用于调用guesser::guesser()

我试着用与providerobj相同的方法来实现它,但由于某些原因,它不起作用。 这是猜测者的代码

guesser.cpp

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

using namespace std;

const char FILL_CHARACTER = '.';
string wordSoFar;
int numMissedGuesses;
const int MAX_MISTAKE_LIMIT = 10;

void Game::setUpGame (int wordLength)
{
    wordSoFar = string(wordLength, FILL_CHARACTER);
    numMissedGuesses = 0;
}


bool Game::guesserHasWon()
{
    return wordSoFar.find(FILL_CHARACTER) == string::npos;
}

bool Game::guesserHasLost()
{
    return numMissedGuesses >= MAX_MISTAKE_LIMIT;
}


void Game::guessHasBeenMade (char guess)
{
    bool isInWord;
    Provider::getResponseToGuess(guess, isInWord, wordSoFar);
    if (isInWord)
    {
        characterIsInWord (guess, wordSoFar);
    }
    else
    {
        ++numMissedGuesses;
        characterIsNotInWord (guess);
    }
}
#include "provider.h"
#include "game.h"

#include <iostream>


using namespace std;


int Provider::initialPrompt ()
{
  cout << "Let's play Hangman!\n\n"
       << "Please think of a word from 4-9 characters long. The word\n"
       << "should not be a proper name (something that you would normally\n"
       << "capitalize, nor should it contain any punctuation characters.\n"
       << "\n\nOK, got a word?\n" << endl;
  int len = 1;
  while (len < 4 || len > 9)
    {
      cout << "How long is your word? " << flush;
      cin >> len;
      if (len < 4 || len > 9)
    {
      cout << "Please choose a word between 4 and 9 characters long."
           << endl;
    }
    }
  return len;
}



bool getYesNoResponse()
{
  string response;
  getline (cin, response);
  while (response.size() == 0 || 
     (response[0] != 'y' && response[0] != 'Y'
      && response[0] != 'n' && response[0] != 'N'))
    {
      if (response.size() > 0)
    cout << "Please respond 'yes' or 'no'. " << flush;
      getline (cin, response);
    }
  return response[0] == 'y' || response[0] == 'Y';  
}


void Provider::getResponseToGuess (char guess, bool& isInWord,
                   std::string& wordSoFar,
                   int numMissedGuesses)
{
  cout << "I have missed " << numMissedGuesses << " guesses ("
       << Game::MAX_MISTAKE_LIMIT - numMissedGuesses << " misses left)"
       << endl; 
  cout << "\n" << wordSoFar << "\n";
  for (int i = 1; i <= wordSoFar.size(); ++i)
    cout << i;
  cout << "\n\nDoes your word contain the letter '" 
       << guess << "'? (y/n) " << flush;
  isInWord = getYesNoResponse();
  if (isInWord) {
      string response;
      bool done = false;
      string newWord;
      while (!done)
    {
      cout << "Enter all of the character positions (1-"
           << wordSoFar.size() << ") in which the letter '"
           << guess <<  "' appears: " << flush;
      getline (cin, response);
      bool digitsFound = false;
      newWord = wordSoFar;
      for (int i = 0; i < response.size(); ++i)
        {
          char d = response[i];
          if (d >= '1' && d <= '0' + wordSoFar.size())
        {
          int k = d - '1';
          if (wordSoFar[k] == Game::FILL_CHARACTER)
            {
              newWord[k] = guess;
              digitsFound = true;
            }
        }
        }
      if (digitsFound)
        {
          cout << "Like this: " << newWord << "? (y/n) " << flush;
          bool yn = getYesNoResponse();
          if (yn)
        {
          wordSoFar = newWord;
          done = true;
        }
        }
    }
  }
}



/**
 * Announce that the provider has won the game, and get the
 * provider's actual word.
 */
std::string Provider::providerHasWon ()
{
  cout << "Congratulations, you have won." << endl;
  cout << "\nOut of curiosity, what was your word? " << flush;

  string answer;
  getline (cin, answer);
  return answer;
}


/**
 * Announce that the guesser has won the game, and get the
 * provider's actual word.
 */
void Provider::providerHasLost (string wordSoFar)
{
  cout << wordSoFar
       << "\n\nI have won!\nThanks for playing" << endl;
}
#include "guesser.h"
#include "game.h"

#include <iostream>
#include <fstream>


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 (wordListFilename);
  while (in >> 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;

}


/**
 * 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 Guesser::characterIsNotInWord (char guess)
{
  vector<string> remainingSolutions;
  for (int i = 0; i < possibleSolutions.size(); ++i)
    {
      string wd = possibleSolutions[i];
      if (wd.find(guess) == string::npos)
    {
      remainingSolutions.push_back (wd);
    }
    }
  possibleSolutions = remainingSolutions;
}


/**
 * Guesser has lost the game. Look at the provider's actual word
 * and gripe a bit about losing.
 */
void Guesser::admitToLoss (std::string actualWord, const string& wordSoFar)
{
  bool match = actualWord.size() == wordSoFar.size();
  for (int i = 0; match && i < actualWord.size(); ++i)
    {
      match = wordSoFar[i] == Game::FILL_CHARACTER || 
    wordSoFar[i] == actualWord[i];
    }
  if (!match)
    {
      cout << "Ummm...your word '" << actualWord
       << "' does not match the patterh '"
       << wordSoFar <<"'.\nDid you make a mistake somewhere?"
       << endl;
    }
  else
    {
      for (int i = 0; match && i < actualWord.size(); ++i)
    {
      if (wordSoFar[i] == Game::FILL_CHARACTER
          && charactersTried[actualWord[i]-'a'])
        {
          cout << "Did you forget to mention the '"
           << actualWord[i]
           << "' in position " << i+1 << "?"
           << endl;
          return;
        }
    }

      for (int i = 0; (!match) && i < possibleSolutions.size(); ++i)
    match = (actualWord == possibleSolutions[i]);
      match = match && (possibleSolutions.size() > 0);
      if (match)
    {
      cout << "OK, I might have guessed that eventually." << endl;
    }
      else
    {
      cout << "Interesting, I don't know that word. Are you sure you\n"
           << "spelled it correctly?." << endl;
    }

    }
}
#包括“guesser.h”
#包括“game.h”
#包括
#包括
使用名称空间std;
常量std::字符串猜测器::alphabet=“abcdefghijklmnopqrstuvxyz”;
//使用指定的字长初始化猜谜游戏,
//使用指定文件中的单词。
Guesser::Guesser(int-wordLength,const-char*wordListFilename)
{
对于(int i=0;i<26;++i)
charactersTried[i]=假;
字符串字;
ifstream-in(wordListFilename);
while(在>>word中)
{
if(word.size()==字长)
{
//单词的长度符合要求
if(word.find_first_not_of(alphabet)=string::npos){
//word只包含较低的字母
可能的解决办法。推回(单词);
}
}
}
in.close();
}
/**
*扫描到目前为止可能的解决方案,计算
*每个尚未尝试的字母,包含该字母的单词数。
*猜猜大多数单词中出现的字母。
*/
字符猜测器::猜测字符()
{
整数计数[26];
对于(int i=0;i<26;++i)
计数[i]=0;
//计算可以找到每个字母的字数
对于(int i=0;ifor(char c='a';cProvider::getResponseToGuess(guess,isInWord,wordSoFar);将不起作用,因为getResponseToGuess不是静态函数

Provider.GetResponseToguess也是错误的,因为Provider是类而不是对象,所以不能用类名调用类的成员函数

您必须首先创建类的对象,然后使用该对象调用成员函数

    Provider provider_obj;
    proveder_obj.getResponseToGuess(guess, isInWord, wordSoFar);

如果C++是新的,我建议先尝试一些基本程序。

你还有其他问题,比如你的常数成员没有被初始化。缩进你的代码可能是个好主意。我只是在这里为你做的,但是这不会修复你的计算机上的文件:你必须包括一个“提供者”。“头文件。我的.cpp文件中包含了一个提供程序.h,但我记得有人告诉我不要在头文件中包含其他.h文件。Chris,你能告诉我如何使用语法吗?我想了解初始化的实际含义(我想我只需要声明一个成员来初始化它就可以了?)我甚至不能在我的h文件中包含一个provider.h,因为出于某种原因,它告诉我“没有这样的文件或目录”,我试过了,它仍然不起作用。我得到了一个错误"调用“Provider::getResponseToGuess”时没有匹配的函数。我正在尝试一系列不同版本的函数,但无法得到一个有效的函数。好吧!我的问题是我没有在getResponseToGuess中传递正确数量的参数!编译器没有提到它,所以我甚至没有想过进行双重检查!添加一个对象非常有效,谢谢!!!我还有另一个同样的问题,但出于某种原因,同样的方法似乎不起作用;