weird redefinition of bool error

This is a discussion on weird redefinition of bool error within the C++ Programming forums, part of the General Programming Boards category; The compiler says there is a redefinition of BOOL bsuccess, at the same line. So basically it is a redefinition ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    416

    weird redefinition of bool error

    The compiler says there is a redefinition of BOOL bsuccess, at the same line. So basically it is a redefinition of itself?

    Code:
    tables.h    4    error: `BOOL bsuccess' previously defined here
    tables.h    4    error: redefinition of `BOOL bsuccess'
    Says they are both on line 4. There are a couple other BOOLs that are having the same error below it. I have some BOOLs in other header files and they don't have this problem. What gives?

    Code:
    BOOL bsuccess = false;
    BOOL bfail = false;

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    My guess is that you are including tables.h twice without include guards.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I don't think inclusion guards help against this. Each cpp file that includes the header, will see this definition again.

    If you really need globals, you should use the extern keyword.

    Code:
    //in header
        extern BOOL success;
    
    //in one cpp file
        BOOL success = false;
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Actually, there are two ways of doing it.

    You could have only one definition, as you've done it. Or you could declare a different instance of success in each source file, by using the static keyword. This is probably not what you want, however, because each variable would be independent, and setting one variable would not effect the others.

    Your code works, however, when only one source file is including the header file.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Thanks guys. I was declaring tables.h in two different files. Once in the .cpp file and again in another header file for some reason.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM

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