user created header files

This is a discussion on user created header files within the C++ Programming forums, part of the General Programming Boards category; Another newbie question..... I put together my first program (actually entered from book) using user defined header files and recieved ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    15

    user created header files

    Another newbie question.....

    I put together my first program (actually entered from book) using user defined header files and recieved a linker error:
    undefined reference to .......

    I had included my header files as the book said like this example

    #include "grade.h"


    I changed it to this and it worked.

    #include "grade.cpp"

    My program is working fine now but I don't understand the discrepancy between the book and what actually worked.

    My understanding is the .h file includes the function declaration while the .cpp is the actual function.

    I can post the code if necessary but I think this is more of an abstract question. Thanks......

  2. #2
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Correct! h. files just indicate what functions you can use. The .c/.cpp files have the real implentation. The erros that you got means that you do not have any implementation for the function that you used in your code.
    When you put #include "file.cpp" you are telling to the compiler to add that file to the compilation process.
    Later the "file.cpp", after compiled, will be linked with your code.
    Nothing more to tell about me...
    Happy day =)

  3. #3
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    User defined headers are simple, you mention the header when you write the driver, but don't mention it when you compile. You mention the implementation when you compile, but don't mention it when you write the driver:
    Code:
    /* a.h */
    void printa ( );
    
    /* b.h */
    void printb ( );
    
    /* a.cpp */
    #include <iostream>
    #include "a.h"
    
    void printa ( ) {
      std::cout<<"A"<<std::endl;
    }
    
    /* b.cpp */
    #include <iostream>
    #include "b.h"
    
    void printb ( ) {
      std::cout<<"B"<<std::endl;
    }
    
    /* main.cpp */
    #include "a.h"
    #include "b.h"
    
    int main ( ) {
      printa();
      printb();
    }
    
    /* Compile like so (your mileage may vary if you don't have GCC */
    % gcc a.cpp b.cpp main.cpp
    % ./a.out
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    15
    Thanks for the quick reply and it does make things a little clearer.

    However I'm still confused on why the book has me include the .h file and not the .cpp. Maybe i'm just looking at it wrong.

    Also I looked back at my source files and realized that I'm including the .h files in the .cpp files.
    example
    // in main program file ?terminology?
    #include grades.cpp

    // in grades.cpp
    #include grades.h

    This is working, but is it the "right" way?

  5. #5
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    You have header files that do all your declarations. Then you have implementation files for those header files that include the header files and define the declarations. Then your main program only has to include the header file, but you need to compile the implementation file and link it with the main program for everything to work.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  6. #6
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Originally posted by twm
    You have header files that do all your declarations. Then you have implementation files for those header files that include the header files and define the declarations. Then your main program only has to include the header file, but you need to compile the implementation file and link it with the main program for everything to work.
    In other cases, you jut have the compiled code (a .o or .a file, for example). This happens when you download libraries of people that have their own reasons to "hide" the code.
    Nothing more to tell about me...
    Happy day =)

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    15
    Ok, Thanks again. It is now working the way the book intended I believe. (including just the .h file in the main program)

    I think the problem was that I wasn't compiling everything at the same time. I guess I need to read up some on dev c++ now that I have a very basic understanding of the language itself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-24-2008, 09:02 AM
  2. user defined header files
    By sidu in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2008, 06:40 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 11:21 AM
  5. Header Files
    By CSK in forum C Programming
    Replies: 8
    Last Post: 10-25-2001, 01:18 AM

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