Thread: Linker Problem with virtual c++

  1. #1
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463

    Linker Problem with virtual c++

    I keep running into problem with the linker eventhough I already put the guard in the header files. Here're the errors that I get

    here's the header
    Code:
    #ifndef __TANK__WAR__
    #define __TANK__WAR__ 
    
    #include <allegro.h>
    #include <cstdlib>
    
    int gameover = 0 ;
    
    // Tank Structure
    
    
    
    struct tagTank
    {
    	int x , y;
    	int dir , speed;
    	int color; 
    	int score;
    } tanks[2];
    
    // Bullet Structure
    struct tagBullet
    { 
    	int x , y; 
    	int alive;
    	int xspd , yspd; 
    } bullets[2];
    
    ... some code
    #endif
    Code:
    1>------ Build started: Project: FirstAllegro, Configuration: Debug Win32 ------
    1>TankWar.obj : error LNK2005: "struct tagBullet * bullets" (?bullets@@3PAUtagBullet@@A) already defined in main.obj
    1>TankWar.obj : error LNK2005: "struct tagTank * tanks" (?tanks@@3PAUtagTank@@A) already defined in main.obj
    1>TankWar.obj : error LNK2005: "int gameover" (?gameover@@3HA) already defined in main.obj
    1>C:\Users\admin\Documents\Visual Studio 2010\Projects\FirstAllegro\Debug\FirstAllegro.exe : fatal error LNK1169: one or more multiply defined symbols found
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    What could cause this? I thought guard would overcome this error.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you really must use global variables, declare them extern in the header, then define them in exactly one source file.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    It works. thanks. I don't really like global variables either. But this is a program from an allegro book for game. I will change it later, too tired now to think.
    "All that we see or seem
    Is but a dream within a dream." - Poe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-28-2009, 09:25 AM
  2. Linker problem I think
    By 6tr6tr in forum C++ Programming
    Replies: 5
    Last Post: 10-22-2008, 08:26 PM
  3. Linker Problem with Dev C++
    By Brownie in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2008, 07:18 AM
  4. Linker problem
    By beene in forum Game Programming
    Replies: 1
    Last Post: 11-26-2006, 08:07 AM
  5. Visual Studio Linker problem or my problem?
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-13-2004, 12:32 AM