.h file help

This is a discussion on .h file help within the C++ Programming forums, part of the General Programming Boards category; This is going to sound stupid but... I forgot how to use .h files in basic c++ programs. I know ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    .h file help

    This is going to sound stupid but... I forgot how to use .h files in basic c++ programs.

    I know how to declare the .h file and use the functions, but I forgot how to make the function in the .h file.

    Like what is the basic code for it?

  2. #2
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91
    main.cpp ->
    Code:
    #include "main.h"
    
    int main()
    {
    	Hello();
    
    	return 0;
    }
    main.h ->
    Code:
    #include <iostream>
    
    void Hello()
    {
    	std::cout << "Hello World";
    }

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    ahh yes void. Alright thanks a ton!

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    NO NO NO NO NO NO NO NO!

    Headers should not contain non-template implentation.

    Your headers should contain prototypes, type declartions, global consts, things like that.

    foo.h
    Code:
    #ifndef FOO_H_
    #define FOO_H_
    // Header guards to prevent multiple includes in the same complation unit
    
    struct FOO
    {
      int x;
    };
    
    const unsigned int MAX_LENGTH = 20;
    
    // The following function does foo
    void foo(const FOO&);
    
    #endif
    foo.cpp
    Code:
    #include <iostream>
    #include "foo.h"
    
    void foo(const FOO& f)
    {
      std::cout<<f.x<<std::endl;
    }
    main.cpp
    Code:
    #include <iostream>
    #include "foo.h"
    
    int main()
    {
      FOO f = { 10 };
      foo(f);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File being filled with NULLs
    By Tigers! in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2009, 06:28 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  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, 09:19 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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