-
Linking Errors...
Here are the errors I get:
Code:
--------------------Configuration: Hangman - Win32 Debug--------------------
Compiling...
Main.cpp
Linking...
Main.obj : error LNK2005: "struct hangman hang" (?hang@@3Uhangman@@A) already defined in Hangman.obj
Debug/Hangman.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.
Hangman.exe - 2 error(s), 0 warning(s)
Here's all my code... (again...)
Code:
//Main.h: Main .cpp file
#include <iostream>
#include <windows.h>
#include <string.h>
#include "Hangman.h"
int main()
{
cout<<"|-| /-\\ |\\| G |\\/| /-\\ |\\|"<<endl;
hang.LoopProgram();
return 0;
}
Code:
//Hangman.cpp: Constructors, function definitions, etc for Hangman.h
#include <iostream.h>
#include <string.h>
#include <windows.h>
#include "Hangman.h"
//Hangman Constructor
hangman::hangman()
{
hang.loopvar=true;
hang.PickWord();
hang.length=strlen(hang.word);
hang.spaces = new char[ hang.length ];
for(short x=0; x<hang.length; x++)
{
spaces[x]='_';
}
}
//Hangman Destructor
hangman::~hangman()
{
delete[] spaces;
}
//Function that loops the program until told to stop
void hangman::LoopProgram()
{
while(hang.loopvar!=false)
{
hang.BlankSpaces();
hang.Guess();
hang.Check();
}
}
//Function that picks a random word
void hangman::PickWord()
{
srand(GetTickCount());
short random=rand()%15;
switch(random)
{
case 0:
strcpy(hang.word, "corndog");
break;
case 1:
strcpy(hang.word, "computer");
break;
case 2:
strcpy(hang.word, "telephone");
break;
case 3:
strcpy(hang.word, "electric");
break;
case 4:
strcpy(hang.word, "stereo");
break;
case 5:
strcpy(hang.word, "oreo");
break;
case 6:
strcpy(hang.word, "microphone");
break;
case 7:
strcpy(hang.word, "cat");
break;
case 8:
strcpy(hang.word, "moose");
break;
case 9:
strcpy(hang.word, "internet");
break;
case 10:
strcpy(hang.word, "cow");
break;
case 11:
strcpy(hang.word, "potato");
break;
case 12:
strcpy(hang.word, "card");
break;
case 13:
strcpy(hang.word, "oven");
break;
case 14:
strcpy(hang.word, "speakers");
break;
default:
strcpy(hang.word, "earthquake");
}
}
//Function that draws all of the spaces
void hangman::BlankSpaces()
{
for(short x=0; x<hang.length; x++)
{
system("cls");
cout<<spaces[x]<<" ";
}
}
//Function that gets the user's guess
void hangman::Guess()
{
cout<<endl<<"What's your guess?: ";
cin>>hang.letter;
}
//Function that checks to see if the letter is in the word, if the word is complete, or the... man is hanged
//or however you want to put it. Also adds another number to the NumGuesses variable.
void hangman::Check()
{
//Check to see if letter guess is in word
for(short x=0;x<hang.length;x++)
{
if(hang.word[x]==hang.letter)
{
hang.spaces[x]=hang.letter;
}
}
//Check to see if the word is complete
if(strcmp(hang.spaces,hang.word)==0)
{
MessageBox(NULL, "You Won!", hang.word, MB_OK);
hang.loopvar=false;
}
}
Code:
//and lastly...
//Hangman.h: Struct definition
#include <string>
using namespace std;
struct hangman
{
bool loopvar;
char word[10];
int length;
char letter;
char* spaces;
hangman();
~hangman();
void LoopProgram();
void PickWord();
void BlankSpaces();
void Guess();
void Check();
}hang;
Hopefully this should be it for my hangman program. But like some people say even though most of the time it's not true... "Ask and you shall recieve...". Hey, that sounds good, maybe that should be my quote... nah....
:D
-
Use a class instead of a struct
-
that did... absolutely nothing...
Plus a struct is much more appropriate for this case since my members are meant to be public...
thx anyways i guess tho :)
-
Is that really your main file, it doesn't seem like it( due to the include windows.h and the messagebox function) however if I'm wrong I'm wrong. Anyway the error is stating that somewhere in you code you are creatign another object of type hangman named hang and the linker seems to indicate that this is in your main.obj file. However if this really is all your code may I suggest you try rebuiling your entire project, all files.
-
Why what's wrong with it? I just have a bunch of funcitons and put them all in a loop function, and then used the loop function in my main.cpp file. If I shouldn't do that, then what should i change?
-
What I mean by rebuild is that some compilers will only recompile the files you have changed, and not the files that haven't changed. This could be a source of error if you have moved things around in your files without recompiling each one. AKA your linker may be using an old obj file.
-
Allright, remove your initialization of your hangman struct from your header file. Then initialize your struct in your main file, aka place 'hangman hang' inside your main.cpp file. That will take all your problems away.
-
thx, but...
neither of those work... :( ... *sigh*....
-
If you're going to have objects in header files declare them as extern.
-
lol that's exactly what i did before but people told me to take it out... weird...