Linker Problem with virtual c++

This is a discussion on Linker Problem with virtual c++ within the C++ Programming forums, part of the General Programming Boards category; I keep running into problem with the linker eventhough I already put the guard in the header files. Here're the ...

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

    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
    21,632
    If you really must use global variables, declare them extern in the header, then define them in exactly one source file.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    452
    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, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21