From owner-freebsd-questions@FreeBSD.ORG Fri Dec 16 17:01:48 2005 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id DF31716A41F for ; Fri, 16 Dec 2005 17:01:47 +0000 (GMT) (envelope-from keramida@ceid.upatras.gr) Received: from aiolos.otenet.gr (aiolos.otenet.gr [195.170.0.93]) by mx1.FreeBSD.org (Postfix) with ESMTP id 7B16043D46 for ; Fri, 16 Dec 2005 17:01:46 +0000 (GMT) (envelope-from keramida@ceid.upatras.gr) Received: from flame.pc (aris.bedc.ondsl.gr [62.103.39.226]) by aiolos.otenet.gr (8.13.4/8.13.4/Debian-8) with SMTP id jBGH1iGF028975; Fri, 16 Dec 2005 19:01:44 +0200 Received: by flame.pc (Postfix, from userid 1001) id E0A7111457; Fri, 16 Dec 2005 19:00:46 +0200 (EET) Date: Fri, 16 Dec 2005 19:00:46 +0200 From: Giorgos Keramidas To: David Miao , Nathan Vidican Message-ID: <20051216170046.GB2712@flame.pc> 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> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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> Cc: freebsd-questions@freebsd.org Subject: Re: C++ compile error X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 16 Dec 2005 17:01:48 -0000 On 2005-12-16 23:09, David Miao 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 > > 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 +using namespace std; + int main() { cout << "Hello World!" << endl; keramida@flame[18:52]/tmp$ On 2005-12-16 10:31, Nathan Vidican 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" 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 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" :)