Looking for help with header files

All i am getting is undefined reference. I dont know if i am using header files the wrong way or not.
Well all the function are coming up as undefined reference.

1
2
3
4
5
6
7
8
9
10
11
12
C:\Users\x\Desktop\hangman.o:hangman.cpp|| undefined reference to `initialPrompt()'|
C:\Users\x\Desktop\hangman.o:hangman.cpp|| undefined reference to `setUpGame(int)'|
C:\Users\x\Desktop\hangman.o:hangman.cpp|| undefined reference to `setUpGuesser(int, char const*)'|
C:\Users\x\Desktop\hangman.o:hangman.cpp|| undefined reference to `guessACharacter()'|
C:\Users\x\Desktop\hangman.o:hangman.cpp|| undefined reference to `guessHasBeenMade(char)'|
C:\Users\x\Desktop\hangman.o:hangman.cpp|| undefined reference to `guesserHasWon()'|
C:\Users\x\Desktop\hangman.o:hangman.cpp|| undefined reference to `guesserHasLost()'|
C:\Users\x\Desktop\hangman.o:hangman.cpp|| undefined reference to `guesserHasWon()'|
C:\Users\x\Desktop\hangman.o:hangman.cpp|| undefined reference to `providerHasLost()'|
C:\Users\x\Desktop\hangman.o:hangman.cpp|| undefined reference to `providerHasWon()'|
C:\Users\x\Desktop\hangman.o:hangman.cpp|| undefined reference to `admitToLoss(std::string)'|
||=== Build finished: 11 errors, 0 warnings (0 minutes, 0 seconds) ===| 


hangman.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "game.h"
#include "guesser.h"
#include "provider.h"

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

using namespace std;


int main (int argc, char** argv)
{
  int wordSize = initialPrompt();

  setUpGame (wordSize);
  setUpGuesser (wordSize, "words");

  bool gameFinished = false;

  while (!gameFinished) {

    char guess = guessACharacter();

    bool isInWord = false;
    guessHasBeenMade (guess);

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


  if (guesserHasWon())
    providerHasLost();
  else
    {
      string actualWord = providerHasWon();
      admitToLoss (actualWord);
    }


  return 0;
}
 


provider.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>

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

using namespace std;

int 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;
}

void getResponseToGuess (char guess, bool& isInWord,
			 std::string& wordSoFar)
{
  cout << "I have missed " << numMissedGuesses << " guesses ("
       << 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] == 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;
		}
	    }
	}
  }
}



std::string providerHasWon ()
{
  cout << "Congratulations, you have won." << endl;
  cout << "\nOut of curiosity, what was your word? " << flush;

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

void providerHasLost ()
{
  cout << wordSoFar
       << "\n\nI have won!\nThanks for playing" << endl;
}

 


provider.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef PROVIDER_H
#define PROVIDER_H

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

using namespace std;

int initialPrompt ();

void getResponseToGuess (char guess, bool& isInWord,std::string& wordSoFar);

std::string providerHasWon ();

void providerHasLost ();

#endif 


guesser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#ifndef GUESSER_H
#define GUESSER_H

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

using namespace std;

void admitToLoss (string actualWord);

void characterIsInWord (char guess, const string& wordSoFar);

void characterIsNotInWord (char guess);

char guessACharacter();

bool guesserHasLost();

bool guesserHasWon();

void setUpGuesser (int wordLength, const char* wordListFilename);

extern string* possibleSolutions;

extern int numPossibleSolutions;

extern bool charactersTried[26];

extern int numMissedGuesses;

extern const string alphabet;

bool getYesNoResponse();

#endif


Last edited on


guesser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>

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

using namespace std;


bool charactersTried[26];

string* possibleSolutions;

int numPossibleSolutions;

int numMissedGuesses;

const string alphabet = "abcdefghijklmnopqrstuvwxyz";

/**
 * Guesser has lost the game. Look at the provider's actual word
 * and gripe a bit about losing.
 */
void admitToLoss (std::string actualWord)
{
  bool match = actualWord.size() == wordSoFar.size();
  for (int i = 0; match && i < actualWord.size(); ++i)
    {
      match = wordSoFar[i] == 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] == 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 < numPossibleSolutions; ++i)
	match = (actualWord == possibleSolutions[i]);
      match = match && (numPossibleSolutions > 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;
	}

    }
}

void characterIsInWord (char guess, const string& wordSoFar)
{
  string* remainingSolutions = new string[numPossibleSolutions];
  int numRemainingSolutions = 0;
  for (int i = 0; i < numPossibleSolutions; ++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[numRemainingSolutions] = wd;
	  ++numRemainingSolutions;
	}
    }
  delete [] possibleSolutions;
  possibleSolutions = remainingSolutions;
  numPossibleSolutions = numRemainingSolutions;
  // cerr << numRemainingSolutions << "possibilities left" << endl;

}

void characterIsNotInWord (char guess)
{
  string* remainingSolutions = new string[numPossibleSolutions];
  int numRemainingSolutions = 0;
  for (int i = 0; i < numPossibleSolutions; ++i)
    {
      string wd = possibleSolutions[i];
      if (wd.find(guess) == string::npos)
	{
	  remainingSolutions[numRemainingSolutions] = wd;
	  ++numRemainingSolutions;
	}
    }
  delete [] possibleSolutions;
  possibleSolutions = remainingSolutions;
  numPossibleSolutions = numRemainingSolutions;
  // cerr << numRemainingSolutions << "possibilities left" << endl;
}

char 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 < numPossibleSolutions; ++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;
}

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

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

void setUpGuesser (int wordLength, const char* wordListFilename)
{
  for (int i = 0; i < 26; ++i)
    charactersTried[i] = false;

  const int MAXWORDS = 25000;
  possibleSolutions = new string[MAXWORDS];
  numPossibleSolutions = 0;
  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[numPossibleSolutions] = word;
	    ++numPossibleSolutions;
	  }
	}
    }
  in.close();

}

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';
}



game.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef GAME_H
#define GAME_H


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

using namespace std;

void guessHasBeenMade (char guess);

void setUpGame (int wordLength);

extern std::string wordSoFar;

extern const char FILL_CHARACTER;

extern const int MAX_MISTAKE_LIMIT;

#endif




game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>

#include "game.h"
#include "guesser.h"
#include "provider.h"
using namespace std;

std::string wordSoFar;

const char FILL_CHARACTER = '.';

const int MAX_MISTAKE_LIMIT = 10;




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

void guessHasBeenMade (char guess)
{
  bool isInWord;
  getResponseToGuess (guess, isInWord, wordSoFar);
  if (isInWord)
    {
      characterIsInWord (guess, wordSoFar);
    }
  else
    {
      ++numMissedGuesses;
      characterIsNotInWord (guess);
    }
}





Undefined reference to what?

This is not the guessing game forum.
Well all the function are coming up as undefined reference.

1
2
3
4
5
6
7
8
9
10
11
12
C:\Users\x\Desktop\hangman.o:hangman.cpp|| undefined reference to `initialPrompt()'|
C:\Users\x\Desktop\hangman.o:hangman.cpp|| undefined reference to `setUpGame(int)'|
C:\Users\x\Desktop\hangman.o:hangman.cpp|| undefined reference to `setUpGuesser(int, char const*)'|
C:\Users\x\Desktop\hangman.o:hangman.cpp|| undefined reference to `guessACharacter()'|
C:\Users\x\Desktop\hangman.o:hangman.cpp|| undefined reference to `guessHasBeenMade(char)'|
C:\Users\x\Desktop\hangman.o:hangman.cpp|| undefined reference to `guesserHasWon()'|
C:\Users\x\Desktop\hangman.o:hangman.cpp|| undefined reference to `guesserHasLost()'|
C:\Users\x\Desktop\hangman.o:hangman.cpp|| undefined reference to `guesserHasWon()'|
C:\Users\x\Desktop\hangman.o:hangman.cpp|| undefined reference to `providerHasLost()'|
C:\Users\x\Desktop\hangman.o:hangman.cpp|| undefined reference to `providerHasWon()'|
C:\Users\x\Desktop\hangman.o:hangman.cpp|| undefined reference to `admitToLoss(std::string)'|
||=== Build finished: 11 errors, 0 warnings (0 minutes, 0 seconds) ===| 
Last edited on
OP, i get some warnings (signed/unsigned mismatch etc), but I get NO link errors.

edit: and i copied your code 100%, without changing a thing. I'm using VS2010 premium.
Last edited on
OP, what are you using to build your code? Are you telling your compiler to build all of these files? It is not enough to just open the files in an IDE, they must all be associated somehow. In Visual Studio, for example, you add your files to a project and the compiler builds them all together. It sounds like you're not building all of the files.
did you figure out your problem with your code? I think I am in your class. my issue thus far is that the code compiles, but for some reason it does not update the iteration through the letters...

IE it keeps asking if the letter a is in the word, and not proceeding to b and so forth
Topic archived. No new replies allowed.