Re: [SLUG] C++ newbie problems

From: Paul Braman (aeon@tampabay.rr.com)
Date: Thu Jan 09 2003 - 07:31:39 EST


Here's a few pointers that will be useful.

1) Use the filename extension .cpp for your C++ source code files. It's
the easiest to understand and the most portable representation.

2) Never use the "classic iostreams" that you would get with
<iostream.h>. Any proper header file you include (from the C++ Standard
Library) will not have a .h and will have all of its members in the std::
namespace.

3) Never use a "using declaration" such as "using namespace std" if you
can help it. Always specify the namespace std:: wherever you need it.
Alternatively, you might just pick out a few pieces you are really using
to make your code cleaner.

My version of your program follows:

    #include <iostream>
    int main ()
    {
        std::cout << "Welcome to C++!" << std::endl;
        return 0;
    }

Alternatively:

    #include <iostream>
    int main ()
    {
        using std::cout;
        using std::endl;
        cout << "Welcome to C++!" << endl;
        return 0;
    }

Use the line

    g++ -Wall -o welcome welcome.cpp

to compile it. (Yes, this is better than what other people are telling
you since you get a nice executable named "welcome" instead of "a.out" or
whatever other default the compiler chooses. Additionally, the "-Wall"
option warns you of all basic errors and is really helpful.)

Paul Braman
Authorized C++ Developer
aeon@tampabay.rr.com

On 8 Jan 2003, Doug Koobs wrote:

> I'm trying to learn a bit about programming using C++... Here is my
> first program:
>
> //First Program, welcome.C
>
> #include <iostream>
>
> int main()
> {
> cout << "Welcome to C++!\n";
> return 0;
> }
>
> When I type cc welcome.C at the shell, I get the following:
>
> welcome.C: In function `int main()':
> welcome.C:8: `cout' undeclared (first use this function)
> welcome.C:8: (Each undeclared identifier is reported only once for each
> function it appears in.)
>
> After doing some searching on the Internet, it looks like it has
> something to do with namespace (whatever that is), and I need to add a
> line something like:
>
> using namespace std;
>
> I added that, but got even more errors... I'm running gcc-3.2-7. What am
> I doing wrong? Thanks!
>
> Doug
>
>
>
>



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 13:06:41 EDT