Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
So i made this today for my university project
it says unresolved externals
can some one help ?
here is the code
Hangman.h
C++
#include <iostream>
#include <fstream>
#include <ctime>
#include <string>
using namespace std;
class Hangman{
public:
	const char* ar1[1];
	unsigned int WordCount;
	bool AnotherGame = true;
	string* WordArray;
	bool GuessedWord = false;
	unsigned int WrongGuesses = 0;
	bool GameOver = false;

	Hangman(){
		WordCount = CountWords(ar1[1]);
		WordArray = new string[WordCount];
		ReadWords(ar1[1], WordArray);
	}

	~Hangman(){
		delete[] WordArray;
	}
public:
	void gameloop(){
		timein();
		do {
			// Pick a random word to guess and get its length
			int WordChoice = (rand() % WordCount);
			string& CurrWord = WordArray[WordChoice];
			const unsigned int WordLength = CurrWord.length();

			// Allocate an array to specify guessed letters
			bool* Guessed = new bool[WordLength];
			for (unsigned int i = 0; i < WordLength; ++i) {
				Guessed[i] = false;
			}

			// Initialize game-ending variables


			// Main game loop
			do {
				// Clear the screen and output the man
				Clear();
				if (WrongGuesses = 0){
					d0();

				}
				else if (WrongGuesses = 1){
					d1();

				}
				else if (WrongGuesses = 2){
					d2();
				}
				else if (WrongGuesses = 3){
					d3();
				}
				else if (WrongGuesses = 4){
					d4();

				}
				else if (WrongGuesses = 5){
					d5();
				}
				else if (WrongGuesses = 6){
					d6();

				}
				// If there have been 6 wrong gueses or the 
				// word has been guessed then the game is over.
				if (WrongGuesses >= 6) {
					cout << "GAME OVER!! You Lost!" << endl;
					cout << "The Correct Word is = " << CurrWord << endl;
					GameOver = true;
				}
				else if (GuessedWord) {
					cout << CurrWord << endl;
					cout << "Congratulations!!! you guessed the word " << endl;
					GameOver = true;
				}
				else {
					// Output '*' or the letters that have been guessed
					for (unsigned int i = 0; i < WordLength; ++i) {
						if (Guessed[i]) {
							cout << CurrWord[i];
						}
						else {
							cout << 'X';
						}
					}
					cout << endl;
					char Guess = 0;
					// Get the next guess
					cout << "Next Guess? ";
					cin >> Guess;

					// Check whether the guess is in the word and fill in
					bool GuessInWord = false;
					for (unsigned int i = 0; i < WordLength; ++i) {
						if (!Guessed[i] && (CurrWord[i] == tolower(Guess))) {
							Guessed[i] = true;
							GuessInWord = true;
						}
					}

					// Increment wrong guesses, if needed
					if (!GuessInWord) {
						++WrongGuesses;
					}

					// Check whether all of the letters have been guessed
					GuessedWord = true;
					for (unsigned int i = 0; i < WordLength; ++i) {
						if (!Guessed[i]) {
							GuessedWord = false;
						}
					}
				}
			} while (!GameOver);

			delete[] Guessed;

			// Another game?
			cout << "Play Again? (yes/no) ";
			string PlayAgain;
			cin >> PlayAgain;
			if (PlayAgain == "yes" || PlayAgain == "YES") {
				AnotherGame = true;
			}
			if (PlayAgain == "no" || PlayAgain == "NO")
				AnotherGame = false;
			cout << "Thank You For Playing !!!" << endl;


		} while (AnotherGame);
	}
	void timein(){
		time_t Time;
		time(&Time);
		srand((unsigned int)Time);
	}
	void Clear() {

		for (unsigned int i = 0; i < 20; ++i) {
			cout << endl;
		}
	}
	void d0() {

		cout << "" << endl;
		cout << "" << endl;
		cout << "" << endl;
		cout << "" << endl;
		cout << "" << endl;
		cout << "" << endl;
	}
	void d1() {

		cout << "" << endl;
		cout << "  O" << endl;
		cout << "" << endl;
		cout << "" << endl;

	}
	void d2() {

		cout << "" << endl;
		cout << "  O" << endl;
		cout << "/  " << endl;
		cout << "" << endl;

	}
	void d3() {

		cout << "" << endl;
		cout << "  O" << endl;
		cout << "/ |" << endl;
		cout << "" << endl;

	}
	void d4() {

		cout << "" << endl;
		cout << "  O" << endl;
		cout << "/ | \\" << endl;
		cout << "" << endl;

	}

	void d5() {

		cout << "" << endl;
		cout << "  O" << endl;
		cout << "/ | \\" << endl;
		cout << " /" << endl;

	}

	void d6() {

		cout << "" << endl;
		cout << "  O" << endl;
		cout << "/ | \\" << endl;
		cout << " / \\" << endl;

	}
    unsigned int CountWords(const char* FileName);
	void ReadWords(const char* FileName, string* WordArray);
};

// unsigned cause char only for postive numbers 
unsigned int CountWords(const char* FileName) {

	int StringCount = 0;
	ifstream File(FileName);
	while (!File.eof()) {
		string Word;
		File >> Word;
		// Don't count empty strings
		if (!Word.empty()) {
			++StringCount;
		}
	}
	File.close();
	return StringCount;
}

void ReadWords(const char* FileName, string* WordArray) {

	unsigned int StringCount = 0;
	ifstream File(FileName);
	while (!File.eof()) {
		string Word;
		File >> Word;
		// Don't store or count empty strings
		if (!Word.empty()) {
			WordArray[StringCount] = Word;
			++StringCount;
		}
	}
	File.close();
}


C++
main 

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include "Hangman.h"
using namespace std;

int main()
{
	Hangman m1;
	m1.gameloop();

	return 0;
}
Posted
Updated 21-Jun-15 11:09am
v7
Comments
Dave Kreskowiak 21-Jun-15 17:00pm    
Do you really expect someone to guess what the error message is when it crashes?
Omaure 21-Jun-15 17:04pm    
ok then gonna update it
Omaure 21-Jun-15 17:05pm    
here u go ^^
enhzflep 21-Jun-15 18:03pm    
"it says unresolved externals"
Are you sure about that? Have another look.

It says something that includes portions of that text, but generally in programming, the exact error message is required. That was not the exact message you got!

The errors are related to the libraries you need to include to use the functions found in your program.You can easily google for the function/library names and how to ensure they're included in the linking step.
Mohibur Rashid 21-Jun-15 20:12pm    
I didn't go through your code. But you better take a look at your variable ar1. You have screwed up here.

Your CountWords and ReadWords functions are defined as members of your Hangman class but implemented in global space. The implementation must be:
unsigned int Hangman::CountWords(const char* FileName) {
// function body 
}

void Hangman::ReadWords(const char* FileName, string* WordArray) {
// function boy
}


Additionally, the implementation is located in the header file. You should move the code of these functions (and all other member functions) to a source file called Hangman.cpp.
 
Share this answer
 
Comments
Omaure 23-Jun-15 21:14pm    
thnx
Quote:
unsigned int CountWords(const char* FileName) {

Quote:
void ReadWords(const char* FileName, string* WordArray) {
Both the function definitions have to be prefixed by the Hangman:: qualifier, because their are member functions of the Hangman class.


Quote:
if (WrongGuesses = 0){
d0();

}
else if (WrongGuesses = 1){
d1();
//...
These (and the following) are (very probably) wrong, the equality test operator is == in C++ programming language.
 
Share this answer
 
v2
Comments
Omaure 23-Jun-15 21:14pm    
ye i kinda messed up thnx ^^
CPallini 24-Jun-15 3:35am    
You are welcome.
Because you havent included the error messages we all can guess:

Unresolved externals means that your linker dont find the code for these functions to executed. If it is your code you may not implemented the functions, or a minor change in the interface is needed. For external functions you must google which lib to include.
 
Share this answer
 
Comments
Omaure 23-Jun-15 21:15pm    
nah bro am sure that i included everything , anyways it is ok now

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900