<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>Posts tagged c++  - Entropy and Ecstasy</title><link>http://aaron.maenpaa.ca/blog/tags/c++/</link><description>The most recent ranting and ravings of a madman.</description><lastBuildDate>Sun, 22 May 2016 05:32:35 GMT</lastBuildDate><generator>PyRSS2Gen-1.0.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>This Program Has Been Tested: It Works Perfectly!</title><link>http://aaron.maenpaa.ca/blog/entries/2009/01/28/this_program_has_been_tested_it_works_perfectly/</link><description>&lt;p&gt;... or at least that's what I was told when I found &lt;a class="reference external" href="http://www.engin.umd.umich.edu/CIS/course.des/cis400/cpp/hworld.html"&gt;this implementation of Hello World&lt;/a&gt; in C++:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="cp"&gt;#include &amp;lt;iostream.h&amp;gt;&lt;/span&gt;

&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;Hello World!&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Stashed away at the bottom of the page in the section &amp;quot;Program Notes&amp;quot; is this helpful bit of information:&lt;/p&gt;
&lt;blockquote&gt;
This program has been tested, it works perfectly without any errors or warnings.&lt;/blockquote&gt;
&lt;div class="section" id="and-yet-gcc-complains"&gt;
&lt;h3&gt;... and Yet, GCC Complains&lt;/h3&gt;
&lt;p&gt;You may be wondering to yourself &amp;quot;What on Earth prompted them to include that little tidbit of information?&amp;quot;. Even in C++ Hello world should not be too hard to get right:&lt;/p&gt;
&lt;pre class="literal-block"&gt;aaron@athena:~/scratch$ g++ hello.cpp
In file included from /usr/include/c++/4.2/backward/iostream.h:31,
                 from hello.cpp:1:
/usr/include/c++/4.2/backward/backward_warning.h:32:2: warning:
    #warning This file includes at least one deprecated or antiquated
    header. Please consider using one of the 32 headers found in section
    17.4.1.2 of the C++ standard. Examples include substituting the &amp;lt;X&amp;gt;
    header for the &amp;lt;X.h&amp;gt; header for C++ includes, or &amp;lt;iostream&amp;gt; instead
    of the deprecated header &amp;lt;iostream.h&amp;gt;.
    To disable this warning use -Wno-deprecated.
&lt;/pre&gt;
&lt;p&gt;... but GCC still complains. The specifics of what it's complaining about are euclidated nicely in &lt;a class="reference external" href="http://members.gamedev.net/sicrane/articles/iostream.html"&gt;this article&lt;/a&gt; but gist is that &lt;tt class="docutils literal"&gt;&amp;lt;iostream.h&amp;gt;&lt;/tt&gt; references Bjarne Stroustrup's original implementation which is only included (if it's included) in modern compiler distributions for compatibility reasons whereas &lt;tt class="docutils literal"&gt;&amp;lt;iostream&amp;gt;&lt;/tt&gt; is the standardized version which is better, faster, cheaper, more portable and should be used in preference to &lt;tt class="docutils literal"&gt;&amp;lt;iostream.h&amp;gt;&lt;/tt&gt; whenever possible.&lt;/p&gt;
&lt;p&gt;After switching to &lt;tt class="docutils literal"&gt;&amp;lt;iostream&amp;gt;&lt;/tt&gt; and informing the compiler that we're &lt;tt class="docutils literal"&gt;using namespace std&lt;/tt&gt; GCC will happily compile and run our simple program:&lt;/p&gt;
&lt;pre class="literal-block"&gt;aaron@athena:~/scratch$ g++ hello.cpp
aaron@athena:~/scratch$ ./a.out
Hello World!aaron@athena:~/scratch$
&lt;/pre&gt;
&lt;p&gt;For stylistic reasons, I'll add an &lt;tt class="docutils literal"&gt;endl&lt;/tt&gt; at the end of the message, but that's not &lt;em&gt;fatal&lt;/em&gt;. Unfortunately, I'm not quite done. As someone who thinks that my compiler might know a thing or two about code, I like to turn on all of the warnings that my compiler is able to give me. Anything that the compiler is worried enough to raise a warning over is probably worth looking into. Compiling with &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-Wall&lt;/span&gt; &lt;span class="pre"&gt;-pedantic&lt;/span&gt;&lt;/tt&gt; shows us that GCC is still not happy:&lt;/p&gt;
&lt;pre class="literal-block"&gt;aaron@athena:~/scratch$ g++ -Wall -pedantic hello.cpp
hello.cpp:5: error: ISO C++ forbids declaration of ‘main’ with no type
&lt;/pre&gt;
&lt;p&gt;... easily enough fixed, just add an &lt;tt class="docutils literal"&gt;int&lt;/tt&gt; return type to the main function.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="all-fixed-up"&gt;
&lt;h3&gt;All Fixed Up&lt;/h3&gt;
&lt;p&gt;After changing the &lt;tt class="docutils literal"&gt;iostream&lt;/tt&gt; header, adding a namespace declaration, adding a return type and a newline (which is a surprising number of things for &amp;quot;Hello World!&amp;quot; when you think about it) we can now compile with &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-Wall&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-pedantic&lt;/span&gt;&lt;/tt&gt; and GCC won't emit so much as a peep:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span class="cp"&gt;#include &amp;lt;iostream&amp;gt;&lt;/span&gt;

&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;Hello World!&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;pre class="literal-block"&gt;aaron@athena:~/scratch$ g++ -Wall -pedantic hello.cpp
aaron@athena:~/scratch$ ./a.out
Hello World!
&lt;/pre&gt;
&lt;/div&gt;
&lt;div class="section" id="the-moral-of-the-story"&gt;
&lt;h3&gt;The Moral of the Story&lt;/h3&gt;
&lt;p&gt;When your students send you email complaining about your implementation of &amp;quot;Hello World&amp;quot; emitting a slew of obscure errors when compiled, posting a note indicating that your program has been compiled and tested and is bug free is probably not the most productive thing you could do.&lt;/p&gt;
&lt;/div&gt;
</description><guid isPermaLink="true">http://aaron.maenpaa.ca/blog/entries/2009/01/28/this_program_has_been_tested_it_works_perfectly/</guid><pubDate>Wed, 28 Jan 2009 22:10:27 GMT</pubDate></item></channel></rss>