Search

2.7 — Forward declarations and definitions

Take a look at this seemingly innocent sample program:

You would expect this program to produce the result:

The sum of 3 and 4 is: 7

But in fact, it doesn’t compile at all! Visual Studio produces the following compile error:

add.cpp(5) : error C3861: 'add': identifier not found

The reason this program doesn’t compile is because the compiler compiles the contents of code files sequentially. When the compiler reaches the function call to add on line 5 of main, it doesn’t know what add is, because we haven’t defined add until line 9! That produces the error, identifier not found.

Older versions of Visual Studio would produce an additional error:

add.cpp(9) : error C2365: 'add' : redefinition; previous definition was 'formerly unknown identifier'

This is somewhat misleading, given that add wasn’t ever defined in the first place. Despite the redundancy of the second error message, it’s useful to note that it is fairly common for a single error to produce (often redundant) multiple compiler errors or warnings.

Best practice

When addressing compile errors in your programs, always resolve the first error produced first and then compile again.

To fix this problem, we need to address the fact that the compiler doesn’t know what add is. There are two common ways to address the issue.

Option 1: Reorder the function calls

One way to address the issue is to reorder the function calls so add is defined before main:

That way, by the time main calls add, the compiler will already know what add is. Because this is such a simple program, this change is relatively easy to do. However, in a larger program, it can be tedious trying to figure out which functions call which other functions (and in what order) so they can be declared sequentially.

Furthermore, this option is not always possible. Let’s say we’re writing a program that has two functions A and B. If function A calls function B, and function B calls function A, then there’s no way to order the functions in a way that will make the compiler happy. If you define A first, the compiler will complain it doesn’t know what B is. If you define B first, the compiler will complain that it doesn’t know what A is.

Option 2: Use a forward declaration

We can also fix this by using a forward declaration.

A forward declaration allows us to tell the compiler about the existence of an identifier before actually defining the identifier.

In the case of functions, this allows us to tell the compiler about the existence of a function before we define the function’s body. This way, when the compiler encounters a call to the function, it’ll understand that we’re making a function call, and can check to ensure we’re calling the function correctly, even if it doesn’t yet know how or where the function is defined.

To write a forward declaration for a function, we use a declaration statement called a function prototype. The function prototype consists of the function’s return type, name, parameters, but no function body (the curly braces and everything in between them), terminated with a semicolon.

Here’s a function prototype for the add function:

Now, here’s our original program that didn’t compile, using a function prototype as a forward declaration for function add:

Now when the compiler reaches the call to add in main, it will know what add looks like (a function that takes two integer parameters and returns an integer), and it won’t complain.

It is worth noting that function prototypes do not need to specify the names of the parameters. In the above code, you can also forward declare your function like this:

However, we prefer to name our parameters (using the same names as the actual function), because it allows you to understand what the function parameters are just by looking at the prototype. Otherwise, you’ll have to locate the function definition.

Best practice

When defining function prototypes, keep the parameter names. You can easily create forward declarations by using copy/paste on your function declaration. Don’t forget the semicolon on the end.

Forgetting the function body

New programmers often wonder what happens if forward declare a function but do not define it.

The answer is: it depends. If a forward declaration is made, but the function is never called, the program will compile and run fine. However, if a forward declaration is made and the function is called, but the program never defines the function, the program will compile okay, but the linker will complain that it can’t resolve the function call.

Consider the following program:

In this program, we forward declare add, and we call add, but we never define add anywhere. When we try and compile this program, Visual Studio produces the following message:

Compiling...
add.cpp
Linking...
add.obj : error LNK2001: unresolved external symbol "int __cdecl add(int,int)" (?add@@YAHHH@Z)
add.exe : fatal error LNK1120: 1 unresolved externals

As you can see, the program compiled okay, but it failed at the link stage because int add(int, int) was never defined.

Other types of forward declarations

Forward declarations are most often used with functions. However, forward declarations can also be used with other identifiers in C++, such as variables and user-defined types. Variables and user-defined types have a different syntax for forward declaration, so we’ll cover these in future lessons.

Declarations vs. definitions

In C++, you’ll often hear the words “declaration” and “definition” used, often interchangeably. What do they mean? You now have enough of a framework to understand the difference between the two.

A definition actually implements (for functions or types) or instantiates (for variables) the identifier. Here are some examples of definitions:

A definition is needed to satisfy the linker. If you use an identifier without providing a definition, the linker will error.

The one definition rule (or ODR for short) is a well-known rule in C++. The ODR has three parts:

  1. Within a given file, a function, object, type, or template can only have one definition.
  2. Within a given program, an object or normal function can only have one definition. This distinction is made because programs can have more than one file (we’ll cover this in the next lesson).
  3. Within a given program, types, template functions, and inline functions can have multiple definitions so long as they are identical. We haven’t covered what most of these things are yet, so don’t worry about this for now -- we’ll bring it back up when it’s relevant.

Violating part 1 of the ODR will cause a compile to issue a redefinition error. Violating ODR parts 2 or 3 will cause the linker to issue a redefinition error. Here’s an example of a violation of part 1:

Because the above program violates ODR part 1, this causes the Visual Studio compiler to issue the following compile errors:

project3.cpp(9): error C2084: function 'int add(int,int)' already has a body
project3.cpp(3): note: see previous definition of 'add'
project3.cpp(16): error C2086: 'int x': redefinition
project3.cpp(15): note: see declaration of 'x'

A declaration is a statement that tells the compiler about the existence of an identifier and its type information. Here are some examples of declarations:

A declaration is all that is needed to satisfy the compiler. This is why we can use a forward declaration to tell the compiler about an identifier that isn’t actually defined until later.

In C++, all definitions also serve as declarations. This is why int x appears in our examples for both definitions and declarations. Since int x is a definition, it’s a declaration too. In most cases, a definition serves our purposes, as it satisfies both the compiler and linker. We only need to provide an explicit declaration when we want to use an identifier before it has been defined.

While it is true that all definitions are declarations, the converse is not true: all declarations are not definitions. An example of this is the function prototype -- it satisfies the compiler, but not the linker. These declarations that aren’t definitions are called pure declarations. Other types of pure declarations include forward declarations for variables and type declarations (you will encounter these in future lessons, no need to worry about them now).

The ODR doesn’t apply to pure declarations (it’s the one definition rule, not the one declaration rule), so you can have as many pure declarations for an identifier as you desire (although having more than one is redundant).

Author's note

In common language, the term “declaration” is typically used to mean “a pure declaration”, and “definition” is used to mean “a definition that also serves as a declaration”. Thus, we’d typically call int x; a definition, even though it both a definition and a declaration.

Quiz time

Question #1

What is a function prototype?

Show Solution

Question #2

What is a forward declaration?

Show Solution

Question #3

How do we declare a forward declaration for functions?

Show Solution

Question #4

Write the function prototype for this function (use the preferred form with names):

Show Solution

Question #5

For each of the following programs, state whether they fail to compile, fail to link, or compile and link. If you are not sure, try compiling them!

a)

Show Solution

b)

Show Solution

c)

Show Solution

d)

Show Solution


2.8 -- Programs with multiple code files
Index
2.6 -- Whitespace and basic formatting

118 comments to 2.7 — Forward declarations and definitions

  • techsavvy....aye

    #include <iostream>
    int add(int x, int y)
    {
        return x + y;
    }
    int main()
    {
        using namespace std;
        cout << "The sum of 3 and 4 is: " << add<<
        (3, 4) << endl;
        return 0;
    }

    when i type this program with     <<add<<
                                      (3,4)
    like above why does it give an output of 14 instead of some error.
    if such a thing occurs in a large program how can it be resolved.How can i highlight the code the [/ ] brackets aren't helping?

    • Alex

      It shouldn't give an output of 14, it should give something like 010011724. cout << add will send the address of function add to cout, which prints "01001172" (or whatever address it is for you). (3,4) evaluates to 4, so it prints as 4.

      Generally if this were to occur in a large program, you'd note that the output was wrong and then find the line that was incorrect (possibly with the assistance of the debugger). Then you'd hopefully note that your statement should be cout << add(3,4) instead of cout << add << (3,4) and fix it.

  • kevin wiggins

    why is that variable declaration cause memory allocation and forward declaration does not for variables?

    • Alex

      Variable declarations are also variable definitions. We use these to tell the compiler that we need a variable to be allocated in memory at this point in the code.

      Forward declarations are pure declarations (not definitions), so no memory needs to be allocated for them. Forward declarations just let the compiler know that something exists. They don't allocate memory for that thing.

  • Mark Knut

    You guys are the best! Keep up the great work and the updates!

  • Ponmani

    In your page 1.7 forward declaration, when you talked about declaration and definition,
    you gave examples as int x; for both declaration and definition?

    Is this correct? Declaration is something like x of type int exists and definition when memory comes in picture. Please elaborate author. Muchas Gracias in advance. Great tutorial.

    • Ponmani

      Actually I got it. Pure declarations and most definitions are declarations too! Sorry. I typed the comment premature and did not read the entire page.

  • Mayank

    I declared a function two times with same name,number of arguments, and data type but the compiler didn't give an error.Why?

    • Alex

      Declaring an identifier more than once is not illegal. It's just redundant.

      Note that you can only define your function once. If you try to define it more than once, your compiler will complain.

  • Jeydn Byrd

    There wouldn't happen to be video tutorials, would there?

  • Karel

    Second question of the quiz in 1.7: "DoMath" should preferably and according to 1.4c be named "doMath"... Very nice tutorial and very understandable as well. Thanks so much Alex.

  • Daniel Ricci

    A function prototype is declaration statement that tells the compiler what a function’s return type is, what the name of the function is, and what the types of the function parameters are. A function prototype can be used to forward declare a function. A forward declaration tells the compiler about the existence of a function (or other type of object) in advance of where it is implemented.

    - I dont think there is a difference in terms of its usage.  Declaring a prototype is the act of forward declaring.

  • Catreece

    Handy information! A few things I was wondering about, though. And then I tested them because I realized I could answer my own questions. Huh. Well here's the answers in case anyone else is interested, then. =P

    #1: Does the int add(x, y) always have to be NAMED add(x, y)?

    I've tested and found I can call it addmoose(x, y) without issue.

    #2: Does the "add" part NEED to be part of it?

    moose(x, y) works just fine, apparently the "add" part is just there for good housekeeping and labeling.

    #3: can I have multiple functions named the same thing with different parameters?

    Yes and no I found out. I tried messing around with an add and a multiply function to test where the limits were, this is what I came up with:

    moose(x, y)
    moose(Na, Nb)

    Did not work. As far as it was concerned, the x/y and Na/Nb were identical; what they're called doesn't matter, only the number of parameters.

    moose(x, y)
    moose(Na, Nb, Nc)

    Did work. It doesn't seem to matter that you call it the same thing, so long as it can differentiate between moose(2 parameters) and moose(3 parameters). No, I don't know why I used moose.

    #4: Is it a good idea to name functions the same thing?

    Depends on the situation, I think. I guess if you're multiplying several different things, you could call several functions multiply and just alter the number of parameters. Since they're reusable in different contexts, and multiplying 3 parameters tends to work exactly the same no matter which parameter is being multiplied, so long as you have 3 of them, I suppose it's not that big of a deal.

    On the other hand, it's handy to name more complex functions to be clear about exactly what it is they're supposed to do. add(2 parameters) is pretty obvious that it adds two parameters together, so isn't exactly a big deal if you have add(x, y) and add(x, y, z).

    If you're trying something more complex, such as... pft, I dunno, something like "F = (D* (((E+Y)*(X/N)) *0.01) / (1+0.01* ((E+Y)*(X/N)))", then you might want to stick to a somewhat more descriptive title for the function. You COULD reuse totalNonsense(D, E, X, Y, N) with totalNonsense(X, Y, Z), but I wouldn't particularly suggest it due to issues of confusion.

    Anyway, just the random stuff I came up with when trying to figure out some exceptions to the rules. =3

    • Alex

      Regarding your point #3, C++ supports a concept called "function overloading", whereby you can have multiple functions with the same name so long as no two functions have the same number of parameters of the same type in the same order. I tackle this topic in more detail in section 7.6 -- Function overloading.

      You'll note the following does compile (we haven't introduced double yet, but it's a numeric data type like int):

      Regarding your point 4: as long as the use of the name is intuitive, there's no problem using the same function name for more than one thing.

  • Ramakrishnan

    Hi Alex,

    Really, I am happy and admired about this tutorial. One of the best online tutorial I have ever seen. My question is do you have any idea about BODMAS Theorem. If you know can you please share with us ??

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">