Search

1.9 — Header files

Headers, and their purpose

As programs grow larger and larger (and include more files), it becomes increasingly tedious to have to forward declare every function you want to use that lives in a different file. Wouldn’t it be nice if you could put all your declarations in one place?

C++ code files (with a .cpp extension) are not the only files commonly seen in C++ programs. The other type of file is called a header file, sometimes known as an include file. Header files usually have a .h extension, but you will sometimes see them with a .hpp extension or no extension at all. The purpose of a header file is to hold declarations for other files to use.

Using standard library header files

Consider the following program:

This program prints “Hello, world!” to the console using cout. However, this program never defines cout, so how does the compiler know what cout is? The answer is that cout has been declared in a header file called “iostream”. When we use the line #include <iostream>, we’re requesting that all of the content from the header file named “iostream” be copied into the including file. That makes the content from the header file available for use in our code file.

Keep in mind that header files typically only contain declarations. They do not define how something is implemented. So if cout is only declared in the “iostream” header file, where is it actually defined? It is implemented in the C++ runtime support library, which is automatically linked into your program during the link phase.

Consider what would happen if the iostream header did not exist. Wherever you used std::cout, you would have to manually copy in all of the declarations related to std::cout into the top of each file that used it! This would require knowledge of what’s relevant and what’s not. It’s much easier to just #include iostream!

Writing your own header files

Now let’s go back to the example we were discussing in a previous lesson. When we left off, we had two files, add.cpp and main.cpp, that looked like this:

add.cpp:

main.cpp:

(If you’re recreating this example from scratch, don’t forget to add add.cpp to your project so it gets compiled in).

We used a forward declaration so that the compiler would know what “add” was when compiling main.cpp. As previously mentioned, writing forward declarations for every function you want to use that lives in another file can get tedious quickly.

Header files can relieve us of this burden. A header file only has to be written once, and it can be included in as many files as needed. This also helps with maintenance by minimizing the number of changes that need to be made if a function prototype ever changes (eg. by adding a new parameter).

Writing our own header files is surprisingly easy. Header files consist of two parts.

The first part is called a header guard, which is discussed in the next lesson (on the preprocessor). Header guards prevent a given header file from being #included more than once in the same file.

The second part is the actual content of the .h file, which should be the declarations for all of the functions we want other files to be able to see. Our header files should all have a .h extension, so we’ll call our new header file add.h:

add.h:

In order to use this header file in main.cpp, we have to include it.

main.cpp that includes add.h:

add.cpp stays the same:

When the compiler compiles the #include "add.h" line, it copies the contents of add.h into the current file at that point. Because our add.h contains a function prototype for add(), this prototype is now being used as a forward declaration of add()!

Consequently, our program will compile and link correctly.

Note: When you #include a file, the entire content of the included file is inserted at the point of inclusion.

If you get a compiler error about add.h not being found, make sure the file is really named add.h. Depending on how you created and named it, it’s possible the file could have been named something like “add” (no extension) or “add.h.txt” or “add.hpp”.

If you get a linker error about add() not being defined, make sure you’ve included add.cpp in your project so it compiles properly!

Angled brackets vs quotes

You’re probably curious why we use angled brackets for iostream, and double quotes for add.h. The answer is that angled brackets are used to tell the compiler that we are including a header file that was included with the compiler, so it should look for that header file in the system directories. The double-quotes tell the compiler that this is a header file we are supplying, so it should look for that header file in the current directory containing our source code first. If it doesn’t find the header file there, it will check any other include paths that you’ve specified as part of your compiler/IDE settings. That failing, it will fall back to checking the system directories.

Rule: Use angled brackets to include header files that come with the compiler. Use double quotes to include any other header files.

Header files may include other header files. However, you should never rely on this. Always #include all of the header files you need to use from each .cpp file.

Rule: Each .cpp file should explicitly #include all of the header files it needs to compile.

Why doesn’t iostream have a .h extension?

Another commonly asked question is “why doesn’t iostream (or any of the other standard library header files) have a .h extension?”. The answer is, because iostream.h is a different header file than iostream is! To explain requires a very short history lesson.

When C++ was first created, all of the files in the standard runtime library ended in .h. Life was consistent, and it was good. The original version of cout and cin lived in iostream.h. When the language was standardized by the ANSI committee, they decided to move all of the functions in the runtime library into the std namespace (which is generally a good idea). However, this presented a problem: if they moved all the functions into the std namespace, none of the old programs would work any more!

To try to get around this issue and provide backwards compatibility for older programs, a new set of header files was introduced that use the same names but lack the .h extension. These new header files have all their functionality inside the std namespace. This way, older programs that include #include <iostream.h> do not need to be rewritten, and newer programs can #include <iostream>.

When you include a header file from the standard library, make sure you use the non .h version if it exists. Otherwise you will be using a deprecated version of the header that is no longer supported.

In addition, many of the libraries inherited from C that were still useful in C++ were given a c prefix (e.g. stdlib.h became cstdlib). The functionality from these libraries was also moved into the std namespace to help avoid naming collisions.

However, when you write your own header files, you should give them all a .h extension, since you will not be putting your code in the std namespace.

Rule: use the non .h version of a library if it exists, and access the functionality through the std namespace. If the non .h version does not exist, or you are creating your own headers, use the .h version

Including header files from other directories

Another common question involves how to include header files from other directories.

One (bad) way to do this is to include a relative path to the header file you want to include as part of the #include line. For example:

The downside of this approach is that it requires you to reflect your directory structure in your code. If you ever update your directory structure, your code won’t work any more.

A better method is to tell your compiler or IDE that you have a bunch of header files in some other location, so that it will look there when it can’t find them in the current directory. This can generally be done by setting an “include path” or “search directory” in your IDE project settings.

In Visual Studio, you can right click on your project in the Solution Explorer, and choose “Properties”, then the “VC++ Directories” tab. From here, you will see a line called “Include Directories”. Add your include directories there.

In Code::Blocks, go to the Project menu and select “Build Options”, then the “Search directories” tab. Add your include directories there.

Using g++, you can use the -I option to specify an alternate include directory.

The nice thing about this approach is that if you ever change your directory structure, you only have to change a single compiler or IDE setting instead of every code file.

Can I put function or variable definitions in a header file?

You shouldn’t, as it may cause linker errors. We’ll explain why in the upcoming lesson on header guards.

Declarations are okay.

Header file best practices

Here are a few best practices for creating your own header files.

  • Always include header guards.
  • Do not define variables in header files unless they are constants. Header files should generally only be used for declarations.
  • Do not define functions in header files.
  • Each header file should have a specific job, and be as independent as possible. For example, you might put all your declarations related to functionality A in A.h and all your declarations related to functionality B in B.h. That way if you only care about A later, you can just include A.h and not get any of the stuff related to B.
  • Give your header files the same name as the source files they’re associated with (e.g. grades.h goes with grades.cpp).
  • Try to minimize the number of other header files you #include in your header files. Only #include what is necessary.
  • Do not #include .cpp files.
1.10 -- A first look at the preprocessor
Index
1.8a -- Naming conflicts and the std namespace

355 comments to 1.9 — Header files

  • John

    Thanks for an excellent site.

    Would you say that the code shown below is correct? Note that there are two inclusions of <iostream>, once in main.cpp and once in functions.cpp.

    Compiled with: g++ -o test main.cpp functions.cpp

    • Alex

      Yes, outside of the fact that you’ve used “using namespace std;” in the global scope. 🙂

      It’s not only fine to include iostream from both main.cpp and functions.cpp, it’s also necessary.

  • Tudor

    Hi Alex!
    Thank you for the excellent turorials =)

    I’ve followed the section on header files to the letter.
    Here’s my code:

    #include <iostream>
    #include "add.h"

    int main()
    {

        using namespace std;
        cout << "The sum of 3 and 4 is " << add(3, 4) << endl;
        return 0;
    }

    And here’s my error message:

    --------- Build file: "no target" in "no project" (compiler: unknown)----------

    mingw32-g++.exe   -c "C:\Users\Draeforthe\Desktop\My Project\include file test.cpp" -o "C:\Users\Draeforthe\Desktop\My Project\include file test.o"
    mingw32-g++.exe  -o "C:\Users\Draeforthe\Desktop\My Project\include file test.exe" "C:\Users\Draeforthe\Desktop\My Project\include file test.o"  
    C:\Users\Draeforthe\Desktop\My Project\include file test.o:include file test.cpp:(.text+0x27): undefined reference to `add(int, int)’
    collect2.exe: error: ld returned 1 exit status
    Process terminated with status 1 (0 minute(s), 4 second(s))
    2 error(s), 0 warning(s) (0 minute(s), 4 second(s))

    I’m flummoxed. Any help I can get would be appreciated.

    • Alex

      So it looks like your compiler is compiling test.cpp, and then the linker is telling you that it can’t find a definition for function add(). Did you define add() anywhere? If you didn’t, that’s your problem. If you did, then whatever file you defined it in (add.cpp maybe?) isn’t getting compiled into your project.

  • Theo

    Greetings Alex,

    The tutorial is awesome, the best I have read on a programming language.
    I think I followed the instructions correctly but I get this error;
    Fatal error LNK1120: 1 unresolved externals.

    This is my code and where files are located:

    add.h located below  Header Files:

    #ifndef ADD_H
    #define ADD_H

    int add(int x, int y);

    #endif

    main.cpp under Source Files:

    #include "stdafx.h"
    #include <iostream>
    #include "add.h"

    int main()
    {
        using namespace std;
        cout << "The sum of 3 and 4 is: " << add(3, 4) << endl;
        return 0;
    }

    add.cpp under Source Files:

    #include "stdafx.h"
    #include <iostream>

    int add(int x, int y)
    {
        return x + y;
    }

    And two more things, what is #pragma once that Visual studio 2015 writes on Header Files?
    How come we name the header add.h and we write in in capital in #define ADD_H
    Thank you

    • Alex

      I don’t see anything wrong with your code or names, so did you remember to add add.cpp to your project, so that it gets compiled? When doing a full recompile, you should see add.cpp show up in the list of files your compiler is compiling.

      I talk about what #pragma is in either the next lesson or the lesson beyond that one.

      It’s common convention to make your #define values all caps.

      • Theo

        The add.cpp is located under Source Files along with main.cpp. Is this what you mean? Or am I supposed to add it elsewhere as well? I was under the impression that whatever is under Source Files is part of the project. Or do I have it completely wrong?

        • Alex

          If it’s in your project’s source files, it should get compiled. Do you see it compiling when you compile your program?

          If you move function add() from add.cpp to main.cpp, does it compile? If so, then something is going wrong with the compiling and linking of add.cpp.

          • Theo

            Yes it was including add.cpp when i tried to compile it. It gives me the same issue even when I removed add.h and add.cpp and put everything inn main.cpp. Anyway thanks for the help Alex, really appreciate it, I will have a developer friend have a look at it later in the week. It’s will be easier since he will be looking at it from my screen 🙂

  • Rahul

    This one I wrote as main.cpp

    #include "stdafx.h"
    #include<iostream>
    #include "add.h"

    int main()
    {
        std::cout << "The sum of 4 and 4 :"<<
        add(4, 4) << std::endl;
        std::cout << add_h ;
        return 0;
    }

    and add.h and add.cpp is same as you defined, but after compiling I got this error
    error C2059: syntax error: ‘;’
    What is the meaning of this?

    • Alex

      I’m not sure what line you’re getting the error on. The compiler is telling you that it found a ; character where it was expecting something else. I’m guessing that it’s complaining about this line:

      Because this makes no sense. What were you expecting this line to do?

  • Rahul

    Why did you write #define ADD_H after #ifndef ADD_H in add.h?
    What is the use of #define over there?
    And thanks these lessons are great!!

  • Ivar

    What is the reason for this best practice:

    "Try to minimize the number of other header files you #include in your header files. Only #include what is necessary."

    Assuming the header files only include declarations, #including them should not have an effect on memory usage or compile time, right? (Of course it makes no sense to #include stuff you don’t use, I’m just curious if there is a specific reason since it was mentioned explicitly.)

    Thanks for an amazing tutorial!

    • Alex

      There are quite a few reasons for this:
      * It could slow down compilation. All those declarations need to get parsed and stored while the compile is compiling.
      * You could pollute the global namespace, making it a lot more likely you’ll run into naming collision issues (not as much of a problem with the standard library since it’s all in the std namespace).
      * It adds a lot of unnecessary dependencies to your code.
      * Including extra header files hides the real dependencies.

      Or, to answer the question philosophically, if they thought this was a good idea, why wouldn’t they make it easy to do so? 🙂

  • Nishtha

    What is the diff between adding a namespace and header file? Also, you mentioned that when we write include iostream.h, cout function is only defind in this header file and actual definition is somewhere else, but when we made our own header file, y we have added definition also? Y not we have placed it somewhere else.

    • Raquib

      1- If I understand correctly what you are asking is , "if I am already including <iostream> why the hell do I need to add ‘std::identifier’ (recommended way) or ‘using namespace std’ " ? you think so maybe becoz you expect when you #include<iostream> all of these(cin, cout, etc.) identifier to be included. But that is not how it is, in C++ all the identifier are grouped together under the namespace std. You can check the iostream header file where you have installed C++.

      Looks something like-

      namespace std {
        extern istream cin;        /// Linked to standard input
        extern ostream cout;        /// Linked to standard output
        extern ostream cerr;        /// Linked to standard error (unbuffered)
        extern ostream clog;        /// Linked to standard error (buffered);
          …
      }

      So to access cin, cout etc. you need to tell the compiler where these are ( its Scope). That’s what using namespace std, does.

      Also remember, iostream is not the only header file that puts stuff in the namespace std. There are other headerfiles which does as well.

      Apart from that, adding namespace avoids naming conflicts of identifiers, coz you limit the scope of identifier say cin, within the namespace std.

      2. Yes, cout is actually only declared in the iostream headerfile under namespace std. The actual definition is in the runtime support library.
      When you are also making your own headerfile, it should only contain declarations. The actual definition should be in a separate cpp source file. Remember, this is just a recommendation, since it is a good practice.
      Even this tutorial does that, I think you missed it. Here you go ----
      //////////////////////////////////////////////////////////////////////////////////////////
      add.h:

      // This is start of the header guard.  ADD_H can be any unique name.  By convention, we use the name of the header file.
      #ifndef ADD_H
      #define ADD_H

      // This is the content of the .h file, which is where the declarations go
      int add(int x, int y); // function prototype for add.h -- don’t forget the semicolon!

      // This is the end of the header guard
      #endif
      In order to use this header file in main.cpp, we have to include it.

      main.cpp that includes add.h:

      #include <iostream>
      #include "add.h"

      int main()
      {
          using namespace std;
          cout << "The sum of 3 and 4 is " << add(3, 4) << endl;
          return 0;
      }

      add.cpp stays the same:

      int add(int x, int y)
      {
          return x + y;
      }
      //////////////////////////////////////////////////////////
      Regards.

    • Alex

      > What is the diff between adding a namespace and header file?

      These two things are dissimilar enough I’m not sure where you’re getting confused. Can you be more specific about where your confusion is?

      > Also, you mentioned that when we write include iostream.h, cout function is only defind in this header file and actual definition is somewhere else, but when we made our own header file, y we have added definition also? Y not we have placed it somewhere else.

      The definition for std::cout is part of the standard library, which gets linked in as part of the linking phase. For the code you write, you have to supply the definition so that the compiler can compile it.

  • Jim

    Alex & Code::blocks users: Here are a couple of points that I’ve discovered using CB. I’m using CB 16.01.

    1. If you want to enter a header file into codeblocks.

    You need to use File Menu->Empty File, answer yes to add it to the existing project, and save it in the windows dialog box with the file named add.h.

    2. If you have worked on the add function program and added a add.h header file to your program in the project.

    If it  won’t compile make sure you have the released version turned on and not the debug version.

  • Tristan Gybels

    I get it right now 😉 I just read your explanations over and over again.
    Sometimes it’s just hard to understand for some foreign people.

  • Tristan Gybels

    You are a good teacher and you do explain everything well, but this is a hard lesson to me :/
    Possibly because I don’t speak English as a main language.
    More examples would be lovely to see in this chapter.
    Visual parts are the most effective to learn for me.

  • Chris

    Here is my code, thanks for the tutorial
    its very informative and gets straight to
    the point. Its easier to understand than
    my Book on C++.

  • i have 2 projects:  add.cpp and main.cpp

    i added the codes to both

    then i created add.h

    and added the codes in that too

    then i created a new directory and added :
    main.cpp , add.cpp and add.h into that folder(all files of the projects)
    then started main.cpp
    added

    #include "add.cpp"

    into the code and that ran correctly.
    (i didn’t add anything to active projects and everything is in one directory)
    is that fine?
    will it work when my program gets bigger?

    • Alex

      > will it work when my program gets bigger?

      No. You should not #include other .cpp files. You will run into problems later.

      You should create a single project and add files add.cpp and main.cpp to it.

  • Ryan

    Yello!
    Great tutorial!!

    I have the following error:

        C1083 cannot open include file:’add.h’ no such file or directory

  • Nathan

    The only website I would consider turning off ad block for. Thanks for all the hard work you put into this site!

  • Nyap

    So:
    The C++ Standard Library contains all the declerations
    The Standard Runtime Library contains all the definitions
    Correct?

    • Sihoo

      Alex
      February 18, 2016 at 5:25 pm · Reply
      a) No, when people talk about the “iostream library” they’re really talking about the iostream functionality inside the C++ standard library.
      b) The runtime support library is the same thing as the C++ standard library. I’ll update the language in lesson 0.4.

      Above is the author wrote an answer to another question similar to yours.

      So basically, C++ Standard Library is used interchangeably with Standard Runtime Library or Runtime Support Library. When we say C++ standard library, it basically refers to both headers files(declarations) AND corresponding definitions for these declarations, which are already precompiled as binary codes.

      Alex explains in detail about this on Section A.1. Below is the link.

      http://www.learncpp.com/cpp-tutorial/a1-static-and-dynamic-libraries/

  • Carl

    In the add.h file I had the Compile file & Link file boxes checked under Properties/Build and when building the project it threw up the errors mentioned. Removing the checks cured it.

    Hope that clarifies things.

    Once again, a huge thank you for the time and effort on your part for producing such a brilliant site.

    Carl

  • Carl

    Alex

    Absolutely brilliant tutorial - and all for free!!

    Using Code::Blocks 16.01 and had difficulty with 1-9 Header files. Kept getting "ld returned 1 exit status" in the build messages and in the build log references to "add.h.gch file not found". Did a bit of Googling and found it was due to the Compile/Link boxes being checked on the Properties/Build tab.

    Reading down the comments it seems one or two folk have experienced this.  Could you mention it in the tutorial in the bit that talks about compiler error please?

    Or have I missed something? (likely!!)

  • person

    you said in multi file projects, we can add files.
    what would happen if we #include a .cpp file.

    would that do the same thing as adding the file to a project.
    if not, what would it do?

    • Alex

      No, it’s not the same thing as adding a file to the project. When you add a file to the project, it gets compiled as a separate unit. If you #include a .cpp file in another .cpp file, the contents of the included file become part of the includer, and they are treated as one file. I’m not aware of any good reasons to #include .cpp files.

  • Janaka

    You are the Best Teacher I seen in the internet university. Thank you very much.

  • Danny

    do we need to add #include "stdafx.h" inside *.h files or any non .c/.cpp ?

    here my code

    cptutor.cpp

    doubleNumber.cpp

    doubleNumber.h

    i did try to omit #include"stdafx.h" in doubleNumber.h without any noticeable problem, am i doing it right? please explain

    • Alex

      #include “stdafx.h” is only needed when ALL of the following are true:
      * If you are in a .cpp file
      * If you are using Visual Studio
      * If precompiled headers are turned on (they are by default)

  • PurpleKitty

    How can I get Dev C++ to use the auto <> {} "" () instead of me manually have to put both

  • SUMOman

    1st thing that comes to my mind is why iostream is not iostream.cpp(forward declared)…It will be much more efficient than iostream…Some people say that when a program is executed…the exe file will be dependent on the other cpp file during execution in case of forward declarations,which is nonsense because exe file is a machine level language and cpp is a high level language…And i actually copied the exe file   formed from the project to the desktop,deleted the project file and ran the exe file to see if there were any dependencies….Turned out there were no dependencies…Luckily i restored the deleted project from the bin…lol..I dont understand the use of the header file

    • Alex

      > 1st thing that comes to my mind is why iostream is not iostream.cpp(forward declared)…It will be much more efficient than iostream…

      I don’t understand what you’re suggesting.

      “#include ” imports the declarations necessary for your code to use the functionality of iostream (the classes and functions that make up iostream). iostream.cpp is not made available to you -- instead, all you get is the precompiled standard runtime library (which the linker links in).

      • Sihoo

        iostream.cpp is not made available to you -- instead, all you get is the precompiled standard runtime library (which the linker links in).

        Thank you for this explanation. I was spending quite a long time trying to find the cpp files for the library functions. Thought I will be able to find them just like header files, but I guess not since they are not made available to a user.

  • Nathan

    Hi Alex, loving this site!
    I’m still struggling to understand why you would want to use headers instead of forward declarations. It seems to me that creating a new header file AS WELL AS the cpp file just creates a lot of unnecessary work.

    For example, I did your adding example (but with an added function that gets two inputs from the user and adds them), and used different cpp files for each function (so main.cpp, add.cpp and getNumber.cpp), using forward declarations. I then went and redid that program with header files. Both programs do exactly the same thing, but the header file one uses an extra 3 files, each containing an extra 4 lines of code. It seems to me that in the long run this would really get complicated!

    Thanks in advance for your answer, and thanks for the work you put into this site!

    • Alex

      In reality, you’d almost never make one file per function. As you note, this would get really complicated.

      Generally a single code file will represent a collection of related functions, and the header file contains all of the forward declarations for the code in that file. That way, anyone who wants to use the code in that file can #include the header file (one line of code once set up) rather than having to individually copy the function prototype for each function used.

      Furthermore, if you ever update a function’s prototype, the change only needs to be reflected in the code file and the header. That change will be propagated to all #includers. If you manually copy/paste prototypes into your code file, you’d have to update each one individually.

      Also note that so far, we’ve just talked about functions, and functions are relatively straightforward. Later on, we’ll discuss how to create your own new types of data. Those can get really complicated, and you definitely will want to use headers to propagate the forward declarations for those instead of copying the type declarations into multiple files.

      In short, header files don’t really save much work if you’re only using the content of a file in one other file. However, beyond that, they do improve organizational structure, and help prevent redundancy when a function (or other type of object) needs to be included in multiple places.

      • Nathan

        Thanks for the quick reply, I don’t know why I didn’t consider files containing multiple functions. It makes perfect sense now 🙂
        Thanks again for your continued work on this site!

  • moses

    Thanks Alex, runs like a top now.

  • moses

    Alex,
    first thank you for the great tutorial. 🙂
    second here’s my issue. I have compiled all source code according to the tutorial. I know that add.cpp & main.cpp are correctly defined and that add.cpp is included (correctly) in the main solution because main does execute properly when the function prototype (for add.cpp) is used in main. When I execute (so far I’ve always done so using "start without debugging") main without the function prototype but with [#include "add.h"] I get the following errors :
       |Code |Description        |Project |File     |Line
    ============================================================
    abc       cannot open         main     main.cpp  6
              source file
              "add.h"
       c1083  cannot open         main     main.cpp  6
              include file:
              ‘add.h’: No such
              file or directory
    abc       identifier "add" is main     main.cpp  13
              undefined
    =======================================================
    Here’s my code (I’m not sure if I’m using the [] brackets correctly for syntax highlighting but here goes).
    main.cpp
    [//main.cpp : Defines the entry point for the console application.//

    #include "stdafx.h"
    #include <iostream>
    #include "add.h"

    int main()
    {
    using namespace std;
    cout << "the sum of 3 and 4 is :" << add(3, 4) << endl;
    return 0;
    }

    add.h
    [#include "stdafx.h"
    #ifndef ADD_H
    #define ADD_H

    int add(int x, int y);

    #endif]

    add.cpp
    [// add.cpp : Defines the entry point for the console application.//
    #include "stdafx.h"
    #include <iostream>

    int add(int x, int y)
    {
        return x + y
    }

    I’m using visual studios 2015. the only thing I don’t understand is why main isn’t able to open add.h . I also have this included in the main solution as add.h . It shows it as an add.h.cpp file just like it shows the other programs as main.cpp & add.cpp files all under source files in the main solution. It’s worth noting (maybe :)) that I first compiled add.cpp then main.cpp then added add.cpp to main.cpp while main was open by right clicking on "Source Files" then "Add-Existing Item". I then (after successfully compiling and running main.cpp w/the [int add(int x, int y);] function prototype,right clicked again on "Source Files" then selected, "Add-New Item", then wrote the add.h as a cpp file. After seeing that the add.h file had been successfully added in the solution explorer window, I ran the main.cpp as I showed above (without the function prototype and with the [#include "add.h"]). As I said, it might be worth noting that when I went I went to "Add-New Item", the option to create either a new c++ file(.cpp) (creates a file with c++ source code) or a new Header File(.h) (creates a c++ header file) was offered. The first go around I ran w/ c++ file then attempted the illustration above. When that didn’t work I deleted add.h then deleted the [#include "add.h"], and reran main with the function prototype and it was successfully executed. Again I got the output
    "the sum of 3 and 4 is : 7
    Press any key to continue . . ."
    Then I went back through and this time wrote add.h as a header (.h) file. I made sure this was included in the main solution then ran it as prescribed (took out the function prototype and added the #include "add.h") and voila-same thing. Can you help? thanks Alex.

    • Alex

      > It shows it as an add.h.cpp file

      This is the problem. For some reason, your add.h file is named add.h.cpp. It has the wrong extension, so your code can’t find it. Rename it add.h and I think it will work.

  • Andrey

    One has to be amazed with the amount of patience this guy, Alex, demonstrates answering all kinds of questions here over the years 🙂 Great job, man! Also, very neat tutorial - thanks! I do have some Java background so I know my way around when it comes to programming, but C++ has some of its own perks (like this headers for example) and since this tutorial is so well organized, I had no problem finding what I need.
    So, once again - tnx to Alex (& the rest of the crew, if there is one :p )

  • Nico

    i figured it out. well not really, but i got it to work.
    seems like you need to add the files to your project, it does not do this automatically 🙂

  • Nico

    Hi,

    thanks a lot for this tutorial! however:
    i copy/pasted everything, and get the following error:

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter