Thread: Creating Header Files

  1. #1
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964

    Unhappy Creating Header Files

    Hello everyone

    I've been programming C++ casually for about half a year now, and my programs are starting to grow large. I've been monitoring these boards for some time now, and some times i see some of you gurus including and making your own header files for storing away classes and functions.

    There's an entry in the FAQ about making headers, but i just can't get it to work properly, it's quite frustrating, here is my example application:

    Code:
    #include <iostream>
    #include <windows.h>
    #include <string.h>
    #include "function.h"
    
    using namespace std;
    
    int main()
    {
        char sentence1[] = "Why ain't this working!?";
        int length = strlen(sentence1);
        printout(sentence1, length, 100);
    }
    And the "function.h" header:

    Code:
    #ifndef _FUNCTION_H
    #define _FUNCTION_H
    
    inline void printout(char sentence[], int length, int interval)
    {
         int x;
         for (x=0; x<length; x++)
         {
             cout<< sentence[x];
             Sleep(interval);
         }
    }
    
    #endif
    All i get is a compiler error saying that printout is undeclared, i have tried everything, but to no prevail.

    Thanks in advance
    Last edited by Neo1; 09-30-2006 at 01:30 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    I think its because you declare the function before main (it comes before main since it is in a header file)

  3. #3
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Tropicalia:

    Well that doesn't make sense, because if i make a program like this...

    Code:
    #include <iostream>
    #include <windows.h>
    #include <string.h>
    
    using namespace std;
    
    inline void printout(char sentence[], int length, int interval)
    {
         int x;
         for (x=0; x<length; x++)
         {
             cout<< sentence[x];
             Sleep(interval);
         }
    }
    
    int main()
    {
        char sentence1[] = "Why ain't this working!?";
        int length = strlen(sentence1);
        printout(sentence1, length, 100);
    }
    ...it works just fine, and this one declares 'printout' before the main function.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    Its because you must declare the function prototypes

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... for one thing, you might need to #include <windows.h> in functions.h

    Aside from that, have you tried testing with a simpler example?
    Code:
    #include "function.h"
    
    int main()
    {
        printHelloWorld();
    }
    then for function.h:
    Code:
    #ifndef FUNCTION_H
    #define FUNCTION_H
    
    #include <iostream>
    
    void printHelloWorld()
    {
        std::cout << "Hello world!" << std::endl;
    }
    
    #endif
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I think Tropicalia means this:
    Code:
    #ifndef _FUNCTION_H
    #define _FUNCTION_H
    
    inline void printout(char sentence[], int length, int interval);
    
    inline void printout(char sentence[], int length, int interval)
    {
         int x;
         for (x=0; x<length; x++)
         {
             cout<< sentence[x];
             Sleep(interval);
         }
    }
    
    #endif
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    Just do it and your code will work =]

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you're using g++, then try something like
    g++ -E prog.cpp > temp.txt

    The temp.txt file will contain the result of all the #include and #if and #define statements. Look towards the end of the file to see if you're actually getting the file included.

    Other compilers have similar flags for just running the pre-processor.

    > #ifndef _FUNCTION_H
    Pick another name. Everything which begins with _ is reserved, so you should avoid using it yourself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Putting in prototypes like that and expecting it to work is plain silly. There is absolutely no point in having a prototype and immediately defining the function after that. Where prototypes come into play would be having the prototypes in the header file, and then having an implementation file to implement the functions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    I mean put the prototype before main, not in the header, i tested here and worked

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I mean put the prototype before main, not in the header
    That's not very useful either. The thing is, the OP's code should work, with a minor adjustment. Somehow, perhaps due to project settings, the header file is not getting included properly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Laserlight:
    Well that one works fine, aside from the fact that i had to throw in a pause to stop it from exiting instantaneously. But why is my bigger version not working then? I tried including windows.h, but it isn't helping.

    Maxorator:

    The program now looks like this:

    Code:
    #include <iostream>
    #include <windows.h>
    #include <string.h>
    #include "function.h"
    
    using namespace std;
    
    int main()
    {
        char sentence1[] = "Why ain't this working!?";
        int length = strlen(sentence1);
        printout(sentence1, length, 100);
    }
    and the header:
    Code:
    #ifndef _FUNCTION_H
    #define _FUNCTION_H
    
    #include <windows.h>
    
    inline void printout(char sentence[], int length, int interval);
    
    inline void printout(char sentence[], int length, int interval)
    {
         int x;
         for (x=0; x<length; x++)
         {
             cout<< sentence[x];
             Sleep(interval);
         }
    }
    
    #endif
    and it still is not working. Im just getting a "Printout undeclared(first use of this function)"?

    Tropicalia:
    I'm afraid not.

  13. #13
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I've never implemented a function in a header file, so I don't know what's the problem in that code exactly. Just don't use header files for implementation, only declare them there. (As laserlight said)
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah, a test compile of your original code reveals another error: you failed to fully qualify the names. Use std::cout instead of cout.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Salem
    If you're using g++, then try something like
    g++ -E prog.cpp > temp.txt

    The temp.txt file will contain the result of all the #include and #if and #define statements. Look towards the end of the file to see if you're actually getting the file included.

    Other compilers have similar flags for just running the pre-processor.

    > #ifndef _FUNCTION_H
    Pick another name. Everything which begins with _ is reserved, so you should avoid using it yourself.
    I'm assuming G++ is a compiler right? Well i'm using Dev-C++ 4.9.9.2, and i think the compiler is named MingW....

    Where is this temp.txt file located?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help creating multiple text files
    By Tom Bombadil in forum C Programming
    Replies: 19
    Last Post: 03-28-2009, 11:21 AM
  2. Confusion on header and source files
    By dnguyen1022 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2009, 03:42 AM
  3. C Header Files
    By devarishi in forum C Programming
    Replies: 8
    Last Post: 12-10-2008, 04:53 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 09:15 AM