C++ compile error

Giorgos Keramidas keramida at ceid.upatras.gr
Fri Dec 16 09:01:48 PST 2005


On 2005-12-16 23:09, David Miao <davmiao at 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 at 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 at flame[18:51]/tmp$ CC hello.C
    keramida at flame[18:52]/tmp$ ./a.out
    Hello World!
    keramida at 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 at flame[18:52]/tmp$

On 2005-12-16 10:31, Nathan Vidican <nvidican at 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 at 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 at 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" :)



More information about the freebsd-questions mailing list