Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 16 Dec 2005 19:00:46 +0200
From:      Giorgos Keramidas <keramida@ceid.upatras.gr>
To:        David Miao <davmiao@gmail.com>, Nathan Vidican <nvidican@wmptl.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: C++ compile error
Message-ID:  <20051216170046.GB2712@flame.pc>
In-Reply-To: <979f20140512160813i5394e789u1d7624a625e893f@mail.gmail.com> <9061.38.112.155.126.1134747675.squirrel@www.keyslapper.net> <43A2DE53.7080207@wmptl.com> <43A2DDCF.1030708@wmptl.com> <979f20140512160709n3530c01dmdad5714f8e30bc00@mail.gmail.com>
References:  <43A2DE53.7080207@wmptl.com> <979f20140512160813i5394e789u1d7624a625e893f@mail.gmail.com> <979f20140512160709n3530c01dmdad5714f8e30bc00@mail.gmail.com> <43A2DE53.7080207@wmptl.com> <9061.38.112.155.126.1134747675.squirrel@www.keyslapper.net> <979f20140512160709n3530c01dmdad5714f8e30bc00@mail.gmail.com> <43A2DE53.7080207@wmptl.com> <979f20140512160709n3530c01dmdad5714f8e30bc00@mail.gmail.com> <43A2DDCF.1030708@wmptl.com> <979f20140512160709n3530c01dmdad5714f8e30bc00@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On 2005-12-16 23:09, David Miao <davmiao@gmail.com> wrote:
> Dear list,
>
> I try to compile a hello world C++ program in FreeBSD 6.0, but get an
> error as below:
>
> [dm@ORION ~/cpp]% CC -o hello hello.C
> hello.C: In function `int main()':
> hello.C:5: error: `cout' undeclared (first use this function)
> hello.C:5: error: (Each undeclared identifier is reported only once
> for each function it appears in.)
> hello.C:5: error: `endl' undeclared (first use this function)

> ==quote of hello world code==
> #include <iostream>
>
> int main()
> {
>     cout << "Hello World!" << endl;
>
>     return 0;
> }
> == end of quote==

You have to add a line like this:

    using namespace std;

at the toplevel of your sources.  The example then works:

    keramida@flame[18:51]/tmp$ CC hello.C
    keramida@flame[18:52]/tmp$ ./a.out
    Hello World!
    keramida@flame[18:52]/tmp$ diff -u hello.C.orig hello.C
    --- hello.C.orig        Fri Dec 16 18:51:26 2005
    +++ hello.C     Fri Dec 16 18:51:58 2005
    @@ -1,5 +1,7 @@
     #include <iostream>

    +using namespace std;
    +
     int main()
     {
         cout << "Hello World!" << endl;
    keramida@flame[18:52]/tmp$

On 2005-12-16 10:31, Nathan Vidican <nvidican@wmptl.com> wrote:
> gcc assumes a '.C' file is ANSI C, not C++, try:
> mv hello.C hello.cpp && gcc -o hello.exe hello.cpp

Not really.  But it's a very good idea to use something that doesn't
only differ in the case of the name from plain ANCI C, because
transferring the files from Unix to other operating systems
(i.e. Windows) may lose the case of the names some times :-)

On 2005-12-16 10:41, "Louis J. LeBlanc" <FreeBSD@keyslapper.net> wrote:
> I'm not nearly as adept with C++ as I am with C, Perl, and a few other
> geek tools, but doesn't C++ default to the std namespace if none is
> specified?

It doesn't :-(

On 2005-12-17 00:13, David Miao <davmiao@gmail.com> wrote:
> Nathan,
>
> I'm learning c++ programming language by using "The complete c++
> training course - second edition" (Harvey Deitel & Paul Deitel), hello
> world is the first program in this book.  I'm totally puzzled by this
> complex language when I compile my first program. Is this book out of
> date?

That's a pretty old book.  Judging from the sheer number of books these
authors have listed in Amazon, I think it's pretty safe to consider it
very out of date :(

> This post may off topic of FreeBSD, sorry to other people...

That's ok.  The list is, after all, "for general FreeBSD questions" :)




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20051216170046.GB2712>