Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help me with this error!

Status
Not open for further replies.

stillunconscious

Technical User
Oct 28, 2010
2
US
Can someone figure out why I am getting this error? I am not an expert and I just don't see it.

I am getting two errors.

1>hangman.obj : error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl getRandomSecretWord(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?getRandomSecretWord@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV12@@Z) referenced in function _main

: fatal error LNK1120: 1 unresolved externals

And this is what I have...

#include <cassert>
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <time.h>



using namespace std;

const string empty_string = "";


//hangMan prototypes
string getRandomSecretWord(const string& = "secretwords.txt");
string getRandomSecretWords(const string& = "secretwords.txt");
int getNumberOfSecretWords(const string& = "secretwords.txt");
void playHangman(const string&);
string buildMask(int, char = '*');
int computeMaximumWrongGuessCount(const string&);
char readGuessFromUser(const string&);
int countOccurences(char, const string&);
void updateMask(char, const string&, string&);
bool answerIsYes (char, const string&);


//random prototypes
void init(unsigned);
void init();
int nextInt();
int nextInt(int);
int nextInt(int, int);

//string prototypes
string trim(const string&);
string toupper(const string&);

bool answerIsYes(string answer)
{
bool answerIsYes = false;
answer = trim(answer);
answer = toupper(answer);
if(answer[0] == 'Y')

answerIsYes = true;

else answerIsYes = false;

return answerIsYes;
}

int main(int argc, char* argv[])
{
init();//initialize random number generator

cout << "Do you want to play hangman? ";
string answer;
cin >> answer;


answer = trim(answer);
answer = toupper(answer);


while(answer[0] == 'Y')
{
string random_secret_word = getRandomSecretWord();
playHangman(random_secret_word);
cout << "Do you want to play hangman? ";
cin >> answer;
answer = trim(answer);
answer = toupper(answer);
}

}
int getNumberOfSecretWords(const string& file_name)
{
ifstream secret_words;


secret_words.open(file_name.c_str());
assert(secret_words);
int secret_word_counter = 0;
string secret_word;
secret_words>> secret_word;
while(!secret_words.eof())
{
secret_word_counter++;
secret_words >> secret_word;
}
secret_words.close();
secret_words.clear();

return secret_word_counter;
}
string getRandomSecretWord(int secret_word_counter,const string& file_name)
{
ifstream secret_words;
string secret_word;

int secret_word_number = nextInt(1, secret_word_counter);
secret_words.open(file_name.c_str());
assert(secret_words);

for(int counter = 1; counter <= secret_word_number; counter++)
secret_words >> secret_word;
secret_words.close();
return secret_word;
}
void playHangman(const string& original_secret_word)
{
string secret_word = trim(original_secret_word);
int secret_word_length = secret_word.length();
assert(secret_word_length > 0);
secret_word = toupper(secret_word);
string mask = buildMask(secret_word_length);
cout << mask << endl;
int maximum_wrong_guess_count = computeMaximumWrongGuessCount(secret_word);
string already_guessed_list = empty_string;
int wrong_guess_count = 0;
int right_guess_count = 0;


do
{
char guess = readGuessFromUser(already_guessed_list);
int n_occurences = countOccurences(guess, secret_word);
if(n_occurences > 0)
{
right_guess_count = right_guess_count + n_occurences;
already_guessed_list = already_guessed_list + guess;
updateMask(guess, secret_word, mask);
cout << mask << endl;
if(n_occurences == 1)
cout << guess << " appears 1 time in the secret word" << endl;
else
cout << guess << " appears " << n_occurences << " times in the secret word" << endl;
}
else
{

wrong_guess_count = wrong_guess_count + 1;
already_guessed_list = already_guessed_list + guess;
cout << guess << " did not appear in the secret word" << endl;

}
}

while(wrong_guess_count <= maximum_wrong_guess_count &&
right_guess_count < secret_word_length);

if(right_guess_count == secret_word_length)
cout << "You guessed the secret word" << endl;
else
cout << "You did not guess the secret word, "
<< secret_word << endl;

}
string buildMask(int word_length, char mask_char)
{
string mask = empty_string;
for(int counter = 0; counter < word_length; counter++)
mask = mask + mask_char;
return mask;
}
int computeMaximumWrongGuessCount(const string& parameter_word)
{
string word = trim(parameter_word);
int word_length = word.length();
assert(word_length > 0);
if(word_length == 7)
return word_length;
else if(word_length >5)
return word_length-2;
else
return word_length*2;
}
char readGuessFromUser(const string& already_guessed_list)
{

cout << "Please enter a guess: ";
char guess;
cin >> guess;
while(!isalpha(guess))
{
cout<<"You did not enter a letter\n";
cout << "Please enter a guess: ";
cin >> guess;
}
guess = toupper(guess);


while(already_guessed_list.find(guess) != -1)
{
cout << "You already guessed " << guess << endl;
cout << "Please enter a guess: ";
cin >> guess;
while(!isalpha(guess))
{
cout<<"You did not enter a letter\n";
cout << "Please enter a guess: ";
cin >> guess;
}
guess = toupper(guess);
}

return guess;
}
int countOccurences(char guess, const string& secret_word)
{
int n_occurences = 0;
int guess_pos = secret_word.find(guess);
while(guess_pos != -1)
{
n_occurences++;
guess_pos = secret_word.find(guess, guess_pos + 1);
}
return n_occurences;
}
void updateMask(char guess, const string& secret_word, string& mask)
{
int guess_pos = secret_word.find(guess);
while(guess_pos != -1)
{
mask[guess_pos] = guess;
guess_pos = secret_word.find(guess, guess_pos + 1);
}
}
//string functions
string toupper(const string& original)
{
string edited = empty_string;
int length = original.length();
for(int index = 0; index < length; index = index + 1)
{
char symbol = original[index];
if(islower(symbol))
symbol = toupper(symbol);
edited = edited + symbol;
}
return edited;
}
string trim(const string& original)
{
string trimmed = empty_string;
int left = 0;
int right = original.length() - 1;
while(left <= right && isspace(original
))
left = left + 1;
while(left <= right && isspace(original
))
right = right - 1;
if(left <= right)
trimmed = original.substr(left, right - left + 1);
return trimmed;
}
//random functions
void init()
{
srand(unsigned(time(NULL)));
}
void init(unsigned seed)
{
srand(seed);
}
int nextInt()
{
return rand();
}
int nextInt(int upper_bound)
{
assert(upper_bound > 0);
return nextInt() % upper_bound;
}
int nextInt(int lower_bound, int upper_bound)
{
if(lower_bound > upper_bound)
{
int t = lower_bound;
lower_bound = upper_bound;
upper_bound = t;
}
int range = upper_bound - lower_bound + 1;
int next_int = nextInt(range) + lower_bound;
return next_int;
}

 
Your prototype only has one parameter while your definition has two.
 
you declare:

Code:
string getRandomSecretWord(const string& = "secretwords.txt");

and then define it as:

Code:
string getRandomSecretWord(int secret_word_counter,const string& file_name)
{
    ifstream secret_words;
    string secret_word;

so your declaration says it should expect 1 paramter(or zero if you ue the default param), and then you define it as having two paramters. Its complaining the the function youre calling doesnt exist because technically it doesnt.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top