Using CodeBlocks 10.05 – Part 6

This post is a continuation of the series that began with Using CodeBlocks 10.05 – Part 1. The purpose is to help people use CodeBlocks 10.05 with C++ All-In-One Desk Reference For Dummies, which is currently written to rely on CodeBlocks 8.02 (which is actually the previous edition, despite the odd numbering disparity). You can find a complete list of these posts in the C++ All-in-One for Dummies category archive. This post starts with a review of the updates for the source code found in Book IV Chapter 5.

There is a section entitled, “Templatizing a Function” that begins on page 528. You can see the code for this example in \Author\BookIV\Chapter05\FunctionOverloadingAndTemplates. The initial example is designed to fail, as explained on page 531. You’ll see the same error message and recommendations for both versions of CodeBlocks. In order to see this example work, you must comment out the line that reads:

cout << AbsoluteValue(10.5) << endl;

by highlighting the line of code and pressing Ctrl+Shift+C or by choosing Edit > Comment. Of course, you can simply add the comment manually by typing // at the beginning of the line. You must also comment these two functions out:

int AbsoluteValue(int x)
{
    if (x >= 0)
        return x;
    else
        return -x;
}
 
float AbsoluteValue(float x)
{
    if (x >= 0)
        return x;
    else
        return -x;
}

After you comment these lines out, you can uncomment the templated version of the function:

template <typename T> T AbsoluteValue(T x)
{
    if (x >= 0)
        return x;
    else
        return -x;
}

by highlighting the required lines of code and pressing Ctrl+Shift+X or choosing Edit > Uncomment. In addition, you must uncomment these lines of code to exercise the template:

cout << AbsoluteValue<int>(10) << endl;
cout << AbsoluteValue<float>(10.5) << endl;

At this point, the example will compile and run as expected. You’ll receive the same results in both CodeBlocks 8.02 and 10.05. Because so many people have complained about this particular example, I plan to provide two completely different source code files for the next edition of the book (so it won’t be necessary to comment/uncomment anything). Even so, the lines you must uncomment are clearly marked in the current source code file. The example found in the \Author\BookIV\Chapter05\ImFree folder requires similar commenting and uncommenting to see the full potential of the example, but this example doesn’t produce an error at the outset. The other examples in this chapter should work precisely as expected.

On to Book IV Chapter 6 (the last chapter in this mini-book). On page 555 you see Listing 6-8, which contains the code for \Author\BookIV\Chapter06\Sets2. This example won’t currently compile properly and you’ll find the explanation on page 557. In order to run, you must add another library, algorithm, to the group so that the list of includes looks like this:

#include <iostream>
#include <set>
#include <algorithm>

All of the other examples in this chapter run in CodeBlocks 10.05 without a problem on my system (let me know if you experience any problems on your system). In the next post I’ll start
with Book V Chapter 1. In the meantime, let me know if you have any
questions about the CodeBlocks 10.05 updates for this book at John@JohnMuellerBooks.com.