multiple header #includes

This is a discussion on multiple header #includes within the Windows Programming forums, part of the Platform Specific Boards category; my program has multiple .cpp files. in each one, i want to include each of my multiple .h files. but ...

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    multiple header #includes

    my program has multiple .cpp files. in each one, i want to include each of my multiple .h files. but for certain header files, when they are including more than once, i get a linking error, "function already defined in something.obj". Now, i could understand this normally, but in each header file, i've added the code to prevent redefinitions.
    Code:
    #ifndef cBenH
    #define cBenH
    ...
    ...
    ...
    #endif
    here is the controversial header file.
    Code:
    #ifndef hListH
    #define hListH
    
    #include <windows.h>
    
    //Function prototypes
    void Notify(HWND,LPCTSTR);
    void Notify(HWND,long int);
    
    //Functions
    void Notify(HWND hwnd,LPCTSTR str)
    {
    	MessageBox(hwnd,str,"Notification",MB_OK);
    	return;
    }
    
    void Notify(HWND hwnd,long int num)
    {
    	char *chTemp;
    
    	chTemp=(char *)malloc(11);
    	ltoa(num,chTemp,10);
    
    	Notify(hwnd,chTemp);
    
    	free(chTemp);
    	return;
    }
    
    #endif
    why is the code being processed more than once?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    thanks for trying, but i still have the same errors. The problem is, why oh why is the header file being processed more than once? these preprocesser commands make sure it is only ever processed once during a build. but thats not happening.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    first of all, do this:

    Code:
    //your header file (xxxxx.h)
    #ifndef hListH
    #define hListH
    
    #include <windows.h>
    
    //Function prototypes
    void Notify(HWND,LPCTSTR);
    void Notify(HWND,long int);
    
    #endif
    Code:
    //your .cpp file 
    #include <windows.h>
    #include "xxxxx.h"
    
    void Notify(HWND hwnd,LPCTSTR str)
    {
    	MessageBox(hwnd,str,"Notification",MB_OK);
    	return;
    }
    
    void Notify(HWND hwnd,long int num)
    {
    	char *chTemp;
    
    	chTemp=(char *)malloc(11);
    	ltoa(num,chTemp,10);
    
    	Notify(hwnd,chTemp);
    
    	free(chTemp);
    	return;
    }
    i tried an example just like this, it worked and compiled perfectly. i included the .h above in 3 diff .cpp file. one of them was the example .cpp i showed above. this way you're defining in the cpp not the h. in the h you are just declaring.

    if that doesn't work then im going to shoot myself and yourself because it MUST WORK! i just tried it!
    Last edited by Leeman_s; 12-30-2002 at 10:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Precompiled header question
    By donglee in forum C++ Programming
    Replies: 13
    Last Post: 01-22-2009, 01:23 AM
  2. why Multiple define error ...
    By nilathinesh in forum C Programming
    Replies: 2
    Last Post: 10-19-2006, 07:31 AM
  3. Multiple header files , cout undelcared probem
    By rainmanddw in forum C++ Programming
    Replies: 6
    Last Post: 11-22-2005, 10:15 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Replies: 1
    Last Post: 05-01-2003, 03:52 PM

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