Thread: Linking Errors...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    48

    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....
    Hey, you gotta start somewhere

  2. #2
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    Use a class instead of a struct

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    48
    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
    Hey, you gotta start somewhere

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    317
    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.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    48
    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?
    Hey, you gotta start somewhere

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    317
    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.

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    317
    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.

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    48

    thx, but...

    neither of those work... ... *sigh*....
    Hey, you gotta start somewhere

  9. #9
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    If you're going to have objects in header files declare them as extern.

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    48
    lol that's exactly what i did before but people told me to take it out... weird...
    Hey, you gotta start somewhere

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking errors
    By Zeeshan in forum C++ Programming
    Replies: 1
    Last Post: 02-22-2009, 02:10 AM
  2. HELP!!Why and How to solve this linking errors.
    By huwan in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2007, 06:30 AM
  3. Replies: 8
    Last Post: 04-27-2006, 10:39 AM
  4. Linking Errors
    By ForlornOdium in forum C++ Programming
    Replies: 1
    Last Post: 12-07-2003, 10:24 PM
  5. help with allegro - linking errors
    By MadHatter in forum C++ Programming
    Replies: 1
    Last Post: 11-22-2002, 02:01 PM