Thread: Conflicting Header Files

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    288

    Exclamation Conflicting Header Files

    I have 3 files, main.cpp, header1.h and header2.h.

    header1.h only contains a variable: int hello
    header2.h also contains the variable: int hello

    main.cpp includes both headers, when I compile it says multiple definition.

    I know why it does that but I was wondering, is there any way to make the definitions in the header files local to the actual file but global to the functions in it?

    For example:

    header1.h:
    Code:
    int hello;
    
    void func1()
    {
    ...
    }
    
    void func2()
    {
    ...
    }
    both the functions in header1 need to use the variable hello,
    and in header 2:
    Code:
    int hello;
    
    void func1()
    {
    ...
    }
    
    void func2()
    {
    ...
    }
    both the functions in header2 also need to use the variable hello.
    I cannot combine both header files into 1 header because Im splitting the sections of my code into headers in order to update it faster.

    Is there anyway to make the definition of hello in both the headers invisible to main.cpp? but only allowing main.cpp to see the definitions of the funcs and being able to use them.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Namespaces, maybe?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Or use static to give your global variable file scope.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    thanx for the replies guys, i know the question was pretty basic lol, i kinda started out with the advanced before looking into the basics.

    I was wondering, isnt static used to remember the variables previous value?

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    I just tried using static on all my headers, and the compiler still gives the redefinition errors.

  6. #6
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Oh, I thought you were putting it in the .cpp files.
    Since you're putting them all in .h files, use extern.

    header1.h
    Code:
    #ifndef _HEADER1_H_
    #define _HEADER1_H_
    
    extern int hello ;
    
    int getHello()
    {
      return hello ;
    }
    
    #endif
    header2.h
    Code:
    #ifndef _HEADER2_H_
    #define _HEADER2_H_
    
    int hello ;
    
    void setHello( int h )
    {
      hello = h ;
    }
    
    #endif
    main.cpp
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "header1.h"
    #include "header2.h"
    
    int main(int argc, char *argv[])
    {
      setHello ( 4 ) ;
      
      printf( "hello = %d\n", getHello() ) ;
      
      getchar() ;
      
      return 0;
    }

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    I understand what youre trying to explain but I want each header to have its OWN variables but i want all of them to use the same name.

    I have 3 headers, each named after a seperate class: Button, Textbox, and Label.

    To make it easier to understand and read i want each of these headers to use the same names for the variables, e.g. cText instead of buttonText, labelText, and textboxText.

    So far the static thing didnt work, and im not really sure what he meant by namespaces, it sounds kinda advanced. Im jsut looking for a really simple way to do this and im pretty sure it exists.

    If im doing something wrong with the statics can someone show me how to do it right:

    header1
    Code:
    static int hello;
    static char cText;
    
    func1()
    {
    ...
    }
    
    func2()
    {
    ...
    }
    header2
    Code:
    static int hello;
    static char cText;
    
    func1()
    {
    ...
    }
    
    func2()
    {
    ...
    }
    func1 and func2 are made to suit their own headers so I cant have them conflicting with the other headers, they are named differently so its all good except for the variable definitions.

  8. #8
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Ah, I see what you mean. In that case, I would recommend you create corresponding .cpp (or .c) files for your headers.

    Header 1
    Code:
    #ifndef _HEADER1_H_
    #define _HEADER1_H_
    
    int getHello1() ;
    void setHello1( int ) ;
    
    #endif
    Header 2
    Code:
    #ifndef _HEADER2_H_
    #define _HEADER2_H_
    
    void setHello2( int ) ;
    int  getHello2() ;
    
    #endif
    Implementation file for Header 1
    Code:
    #include "header1.h"
    
    static int hello ;
    
    int getHello1()
    {
      return hello ;
    }
    
    void setHello1( int h )
    {
      hello = h ;
    }
    Implementation file for Header 2
    Code:
    #include "header2.h"
    
    static int hello ;
    
    int getHello2()
    {
      return hello ;
    }
    
    void setHello2( int h )
    {
      hello = h ;
    }
    Main Source File
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "header1.h"
    #include "header2.h"
    
    int main(int argc, char *argv[])
    {
      setHello1 ( 1 ) ;
      setHello2 ( 2 ) ;
      
      printf( "1st hello = %d\n", getHello1() ) ;
      printf( "2nd hello = %d\n", getHello2() ) ;  
      
      getchar() ;
      
      return 0;
    }

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    PERFECT thanx man, anyway i can rate you lol? you helped out alot, possibly just saved me from having to rename 100s of variables :S

    thanx again!

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    ok this just got a lil bit more ugly lol i tried converting them but it got VERY confusing, i just messed up all the headers and stuff :S is there anyway to do this without the .c implementation.

  11. #11
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    I don't think you can do it just using header files. Header files (.h) are generally used for declarations, not definitions.

    Declaring (and thus defining) a global variable in header1 and header2 with the same name will always generate a conflict if both headers are included in the same source file.

  12. #12
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Rules of thumb:
    - Never instantiate anything in a header file
    - Never implement code in a header file unless it's within the declaration of a class/struct, a template method/function, or an inline function

    So if you want multiple instances of a variable with the same name, then you need multiple source files, or multiple namespaces within the same source file.

    gg

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    If something goes wrong, it's usually a better idea to figure out what's wrong and fix it rather than try and bypass the problem by doing something else (i.e. just going back to putting everything in the headers).

    What exactly is "ugly"? Errors? Can you post some code?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  14. #14
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    Well i did what Dante recommended and now its not "ugly" any more lol, by that i meant LOTS of errors and the code was very hard to read.

    I now have to include 5 source and 5 header files so my program is getting a little cloggy.

    I was wondering if it is possible to make the code that dante showed into a LIB so that I only have to link the LIB in order to use all the source and header files.

  15. #15
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>I now have to include 5 source and 5 header files
    You split 2 headers into 5? Why?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. classes and header files
    By Drake in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2006, 07:12 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. more header files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-28-2001, 01:56 PM