prob with multiple forms accessing header file

This is a discussion on prob with multiple forms accessing header file within the Windows Programming forums, part of the Platform Specific Boards category; hello there, i have a form(addNotes) accesing a custom header file(Notes_Hdr.h). i easily access this header file with Code: #include ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    10

    prob with multiple forms accessing header file

    hello there,
    i have a form(addNotes) accesing a custom header file(Notes_Hdr.h).
    i easily access this header file with
    Code:
     #include "Notes_Hdr.h"
    my prob is when i use the same code
    Code:
     #include "Notes_Hdr.h"
    on my main form (because i wanna access a funtion in the header file). i get a
    Code:
    error C2011: 'Notes_Hdr' : 'class' type redefinition	d:\my documents\visual studio 2005\projects\electronic diary\electronic diary\Notes_Hdr.h
    error.

    please i need help on how to get this problem fixed or at least find a way to go around it. THANX

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    add #ifndef and #endif to the .h file so when it gets called once, it is used, and when it's called a second time it does not try to redo all the information in the file. Another way is just take the #include "something.h" file out of where it is not needed (some file that is not the main.cpp file).

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    sorry how do i add #ifndef and #endif to the Notes_Hdr.h file?? sorry it might sound a little stupid but i am a newbie to c++

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Code:
    //Very beginning of the .h file
    #ifndef NOTESHDR_H_
    #define NOTESHDR_H_
    
    /*information would go here*/
    
    //Very end of the .h file
    #endif
    Basically means, if not defined (it's one way of looking at it), define whatever name you gave it (NOTESHDR_H_), then do all the stuff between it until endif. If the .h file is called a second time, the NOTESHDR_H_ is already defined once, so it will not do it a second time.

    I usually try to make sure the header file is called only once, so most my header files get included in the main.cpp file.
    Last edited by scwizzo; 11-03-2007 at 01:19 PM.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    THANX!!! i didnt get the error!!!

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    another problem..now that i have included the file successfully on the SECOND form, i have probs calling the functions on the header file from the second form..
    Code:
    Notes_Hdr newNotes;
    
    			 
    			 newNotes.loadNotes();
    it builds successfully..but when i try to build i get a [codeAn unhandled exception of type 'System.AccessViolationException' occurred in Electronic Diary.exe

    Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
    [/code]
    any ideas??

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    When you say forms are you talking about different .cpp files? If you have more than one cpp file that needs to use the same header file, try including the second cpp file to the first, and the first cpp file include the header (don't include the header file on the second .cpp). I'm not sure if that would work or not, though.

    Code:
    //a.cpp
    
    #include <whatever>
    #include "b.cpp"
    #include "notes_header.h"

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    oh yeah sorry about that..that error was related to an array i failed to initialize. Thanks though

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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