我刚刚制作了一个刽子手游戏的代码和idk为什么它不起作用 [英] i just made a code for hangman game and idk why doesnt it work
问题描述
所以我今天为我的大学项目做了这个
它说未解决的外部
可以帮助一些吗?
这里是代码
Hangman.h
So i made this today for my university project
it says unresolved externals
can some one help ?
here is the code
Hangman.h
#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();
}
main
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include "Hangman.h"
using namespace std;
int main()
{
Hangman m1;
m1.gameloop();
return 0;
}
推荐答案
您的CountWords
和ReadWords
函数被定义为Hangman
类的成员,但在全局空间中实现。实现必须是:
YourCountWords
andReadWords
functions are defined as members of yourHangman
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
}
此外,实现位于头文件中。您应该将这些函数(以及所有其他成员函数)的代码移动到名为 Hangman.cpp 的源文件中。
unsigned int CountWords(const char * FileName){
unsigned int CountWords(const char* FileName) {
void ReadWords(const char * FileName,string * WordArray){
void ReadWords(const char* FileName, string* WordArray) {
这两个函数定义都必须以为Hangman ::
限定符,因为它们是 Hangman
类的成员函数。
Both the function definitions have to be prefixed by the Hangman::
qualifier, because their are member functions of the Hangman
class.
if(WrongGuesses = 0){
d0();
}
else if(WrongGuesses = 1){
d1();
// ...
if (WrongGuesses = 0){
d0();
}
else if (WrongGuesses = 1){
d1();
//...
这些(和以下)是(非常可能)错误,等式测试运算符是 ==
in C ++
编程语言。
These (and the following) are (very probably) wrong, the equality test operator is ==
in C++
programming language.
因为哟你还没有包含我们都猜到的错误信息:
未解析的外部意味着你的链接器没有找到要执行的这些函数的代码。如果是您的代码,则可能无法实现这些功能,或者需要对接口进行微小更改。对于外部功能,您必须谷歌包含哪个库。
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.
这篇关于我刚刚制作了一个刽子手游戏的代码和idk为什么它不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!