Creating Header Files

This is a discussion on Creating Header Files within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by laserlight Ah, a test compile of your original code reveals another error: you failed to fully qualify ...

  1. #16
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by laserlight
    Ah, a test compile of your original code reveals another error: you failed to fully qualify the names. Use std::cout instead of cout.
    Everyone was talking about other things, so we never noticed the real error.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  2. #17
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    > #ifndef _FUNCTION_H
    Pick another name. Everything which begins with _ is reserved, so you should avoid using it yourself.
    This helped Everything is working as intended now, thanks for the help!

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Interesting. I cannot duplicate that error in Dev-C++.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by laserlight
    Interesting. I cannot duplicate that error in Dev-C++.
    Really? Are we using the same version?

    Just for clearance:

    This is the faulty version:
    Code:
    #ifndef _FUNCTION_H
    #define _FUNCTION_H
    
    #include <windows.h>
    
    inline void printout(char sentence[], int length, int interval)
    {
         int x;
         for (x=0; x<length; x++)
         {
             cout<< sentence[x];
             Sleep(interval);
         }
    }
    
    #endif
    And the working one:
    Code:
    #ifndef FUNCTION_H
    #define FUNCTION_H
    
    #include <windows.h>
    #include <iostream>
    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);
         }
    }
    
    #endif

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Ah, now I get it. It does look like you have given us a working example where failure to comply with the standard gives you a headache.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by laserlight
    Ah, now I get it. It does look like you have given us a working example where failure to comply with the standard gives you a headache.
    Indeed, as i said, very frustrating...

  7. #22
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    In general, placing C++ code inside header files ( or .h files ) is bad practice. Header files should only really contain function prototypes and class delcarations. All class and function bodys should be stored in the attached C++ source file (.cpp ) This increases program matienence and makes it clearer your intentions

  8. #23
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by swgh
    In general, placing C++ code inside header files ( or .h files ) is bad practice. Header files should only really contain function prototypes and class delcarations. All class and function bodys should be stored in the attached C++ source file (.cpp ) This increases program matienence and makes it clearer your intentions
    How do i "attach" a .cpp file to a header file?

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    You just implement the functions whose prototypes are in the header file, and then compile the source files and link them. If you are using some IDE with some kind of project mechanism, this compilation and linking will all be done for you.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    >> Interesting. I cannot duplicate that error in Dev-C++.

    Seeing how Dev-C++ supposedly isn't a compiler I don't see how any one could.

  11. #26
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by laserlight
    You just implement the functions whose prototypes are in the header file, and then compile the source files and link them. If you are using some IDE with some kind of project mechanism, this compilation and linking will all be done for you.
    Ah, i get it now. Thanks once again.

  12. #27
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,488
    Just one clarification though. Identifiers starting by an underscore are reserved for the implementation. Not for the language itself. Hence two compilers behaving differently but still complying to the standard.

    And there is an important distinction depending on them being followed by an uppercase letter, another underscore or lowercase letter or digit.

    If followed by a lowercase letter or a digit, they are meant to be used by the implementation on the global scope only.

    If followed by anything else they can be used by the implementation anywhere.

    But since these are guidelines to implementation developers, I think it is perhaps a little too pedantic of some to simply deny access to the declaration of any identifier starting with an underscore.
    Last edited by Mario F.; 09-30-2006 at 02:39 PM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #28
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,488
    > Seeing how Dev-C++ supposedly isn't a compiler I don't see how any one could.

    Seeing you can't write code with a compiler, I don't see how you could test it otherwise
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  14. #29
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    This is really giving me a headache!

    I now separated the function prototype and the body of the function into a header and a .cpp file.

    First time around, it worked beautifully, but now im getting all kinds of compiler warnings.

    Here are the files:

    Main
    Code:
    #include <iostream>
    #include <string.h>
    #include "printout.h"
    
    using namespace std;
    
    int main()
    {
        char sentence1[] = "Still ain't working!";
        int length = strlen(sentence1);
        printout(sentence1, length, 100);
        std::cin.ignore();
    }
    Header
    Code:
    #ifndef PRINTOUT_H
    #define PRINTOUT_H
    
    inline void printout(char sentence[], int length, int interval)
    
    #endif
    "attached" source file
    Code:
    #include <windows.h>
    #include <iostream>
    
    inline void printout(char sentence[], int length, int interval)
    {
           int i;
           for (i = 0, i < length, i++)
           std::cout<< sentence[i];
           Sleep(interval);
           }
    }
    expected init-declarator before "using"
    expected `,' or `;' before "using"
    `printout' undeclared (first use this function)


    I just wrote the source files with Dev-C++ in the same project as main and the headers, and as i said, it worked perfectly, but now it just won't compile. I even tried closing Dev-C++ and rewriting the entire application.

    Btw: If i want to inline a function, do i have to inline it in the prototype or in the actual declaration? Or both perhaps?

  15. #30
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,488
    The function cpp file should also include the "printout.h". Otherwise you you include printout.h on your main cpp file, the function definition will not exist.

    Also you have one extraneous closing brace in your function cpp file
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Page 2 of 3 FirstFirst 123 LastLast
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, 02:42 AM
  3. C Header Files
    By devarishi in forum C Programming
    Replies: 8
    Last Post: 12-10-2008, 03:53 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 09:15 AM

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