Simple newbie question..

Discussion in 'C++' started by [ ThrashDK ], Feb 18, 2005.

  1. [ ThrashDK ]

    [ ThrashDK ] Guest

    Hello people,

    What is the difference between "cout" and "std::cout"..? As far as I can
    see, the do the same thing. But I'm just learning, so I might be wrong.. ;)

    --
    Med venlig hilsen / Best Regards - Brian J. Nielsen
    To mail me in private, simply use "email.dk" after the @
    For at maile mig privat, brug da "email.dk" efter @
    [ ThrashDK ], Feb 18, 2005
    #1
    1. Advertising

  2. [ ThrashDK ]

    SirMike Guest

    [ ThrashDK ] wrote:
    > What is the difference between "cout" and "std::cout"..? As far as I can
    > see, the do the same thing. But I'm just learning, so I might be wrong.. ;)

    The first one is without namespace and the second one it with namespace.

    when you use "cout" you must also write somwhere:
    using namespace std;

    std::cout you can use anytime, anywhere

    --
    the code is my strength
    SirMike
    SirMike, Feb 18, 2005
    #2
    1. Advertising

  3. [ ThrashDK ]

    Rolf Magnus Guest

    [ ThrashDK ] wrote:

    > Hello people,
    >
    > What is the difference between "cout" and "std::cout"..?


    cout is the unqualified name, std::cout the qualified one.

    > As far as I can see, the do the same thing.


    They don't:

    #include <iostream>

    int main()
    {
    cout << "Hello, ";
    std::cout << "world\n";
    }

    The first cout line should produce an error message because the name cout is
    not known in the global namespace. The second line should be ok, because
    cout is defined in namespace std.

    #include <iostream>

    int main()
    {
    using std::cout;
    cout << "Hello, ";
    std::cout << "world\n";
    }

    Now, std::cout is imported from namespace std, so it can be used as simply
    cout, too. In this case, cout and std::cout are just to ways to name the
    same object.

    > But I'm just learning, so I might be wrong..
    > ;)


    You just have to read up on namespaces.
    Rolf Magnus, Feb 18, 2005
    #3
  4. [ ThrashDK ]

    [ ThrashDK ] Guest

    Hey SirMike,

    > The first one is without namespace and the second one it with namespace.
    >
    > when you use "cout" you must also write somwhere:
    > using namespace std;
    >
    > std::cout you can use anytime, anywhere


    Okay, thanks a lot. ;) Though I'm not sure I follow. I have this little
    piece of simple code, quoted below. This works just as good, whether I use
    "cout" or "std::cout" - here shown with just "cout". And nowhere is the line
    "using namespace std;". Why is this?


    -----------------------------------
    #include <iostream>

    void DemonstrationFunction()
    {
    cout<<"In function\n";
    }

    int main()
    {
    cout<<"In main\n";
    DemonstrationFunction();
    cout<<"Back in main\n";
    return 0;
    }
    ----------------------------------

    --
    Med venlig hilsen / Best Regards - Brian J. Nielsen
    To mail me in private, simply use "email.dk" after the @
    For at maile mig privat, brug da "email.dk" efter @
    [ ThrashDK ], Feb 18, 2005
    #4
  5. [ ThrashDK ]

    [ ThrashDK ] Guest

    Hey Rolf,

    Okay, I understand that if you want to use just "cout" you have to provide
    the line "using std::cout;". But I have a little piece of code (see answer
    to SirMike), that I tried to play with. When saved, compiled and linked,
    they do the same thing, and I have not provided a line that tells that I can
    just use "cout".. Why? Is it something the compiler "asumes" or just does
    automatically?

    I try not to be too pesky, I'll probably see the light soon. ;)

    --
    Med venlig hilsen / Best Regards - Brian J. Nielsen
    To mail me in private, simply use "email.dk" after the @
    For at maile mig privat, brug da "email.dk" efter @
    [ ThrashDK ], Feb 18, 2005
    #5
  6. [ ThrashDK ]

    SirMike Guest

    [ ThrashDK ] wrote:
    > Is it something the compiler "asumes" or just does
    > automatically?

    Which compiler do you use ?

    --
    the code is my strength
    SirMike
    SirMike, Feb 18, 2005
    #6
  7. [ ThrashDK ]

    Rolf Magnus Guest

    [ ThrashDK ] wrote:

    > Okay, thanks a lot. ;) Though I'm not sure I follow. I have this little
    > piece of simple code, quoted below. This works just as good, whether I use
    > "cout" or "std::cout" - here shown with just "cout". And nowhere is the
    > line "using namespace std;". Why is this?
    >
    >
    > -----------------------------------
    > #include <iostream>
    >
    > void DemonstrationFunction()
    > {
    > cout<<"In function\n";
    > }
    >
    > int main()
    > {
    > cout<<"In main\n";
    > DemonstrationFunction();
    > cout<<"Back in main\n";
    > return 0;
    > }
    > ----------------------------------


    A C++ standard compliant compiler is not supposed to accept that, so if
    yours does, it's broken.
    Some rather old versions of g++ (before 3.x) had such a bug. If you're using
    g++ and care for standard compliance, I strongly suggest upgrading to a
    more recent version.
    Rolf Magnus, Feb 18, 2005
    #7
  8. [ ThrashDK ]

    [ ThrashDK ] Guest

    >> Is it something the compiler "asumes"
    >> or just does automatically?

    > Which compiler do you use ?


    Um, it's rather old, I see.

    Bloodshed Dev-C++ v4.01 (Build on: 22-11-2000)

    As far as I know, it a development enviroment, with a compiler built-in. I
    gues... Is it buggy? It's because I follow a book on C++, and it said to use
    that compiler.

    --
    Med venlig hilsen / Best Regards - Brian J. Nielsen
    To mail me in private, simply use "email.dk" after the @
    For at maile mig privat, brug da "email.dk" efter @
    [ ThrashDK ], Feb 18, 2005
    #8
  9. [ ThrashDK ]

    SirMike Guest

    [ ThrashDK ] wrote:
    > Bloodshed Dev-C++ v4.01 (Build on: 22-11-2000)

    I got also Dev-C++ 4 but the code you wrote doesn't work.
    The compiler (not IDE) must be different. Try to download thee newest
    version of Dev from http://ftp1.sourceforge.net/dev-cpp/devcpp4.zip and
    try to compile it.

    --
    the code is my strength
    SirMike
    SirMike, Feb 18, 2005
    #9
  10. [ ThrashDK ]

    [ ThrashDK ] Guest

    > The compiler (not IDE) must be different. Try
    > to download thee newest version of Dev


    I downloaded the file you linked to, and installed it. I can see in the
    About box that the compiler is different from the one in mine (v4.01 - but
    older compiler???). However, the compiler still accepts the code that should
    not be accepted. But the resulting .exe file is smaller in size with the new
    one, 73728 bytes. When I compile with mine, the older one, the size is
    bigger, 156417 bytes. It seems the compiler in the one you linked to, is
    more efficient, or what? But I guess that's a whole other matter - there is
    still no errors. It compiles, when it apparantly should not..

    ? ? :-//

    --
    Med venlig hilsen / Best Regards - Brian J. Nielsen
    To mail me in private, simply use "email.dk" after the @
    For at maile mig privat, brug da "email.dk" efter @
    [ ThrashDK ], Feb 18, 2005
    #10
  11. [ ThrashDK ]

    [ ThrashDK ] Guest

    > But the resulting .exe file is smaller
    > in size with the new one, 73728 bytes.


    I figured out, that this happens when you tick "Generate Debugging
    Information" in the linker, then the file is bigger.

    And I have now tried it on two different PC's. It compiles with no errors on
    both. I have had a look in the different settings, to see if something was
    set incorrect, but I'm not sure. It doesn't appear to..

    --
    Med venlig hilsen / Best Regards - Brian J. Nielsen
    To mail me in private, simply use "email.dk" after the @
    For at maile mig privat, brug da "email.dk" efter @
    [ ThrashDK ], Feb 18, 2005
    #11
  12. [ ThrashDK ]

    SirMike Guest

    [ ThrashDK ] wrote:
    > I figured out, that this happens when you tick "Generate Debugging
    > Information" in the linker, then the file is bigger.

    Ofcourse but debugging information are not usable by end-user so you
    shouldn't compile it with debugging information when you release the
    final program. But it doesn't matter in your problem.

    --
    the code is my strength
    SirMike
    SirMike, Feb 18, 2005
    #12
  13. [ ThrashDK ]

    Rolf Magnus Guest

    SirMike wrote:

    > [ ThrashDK ] wrote:
    >> I figured out, that this happens when you tick "Generate Debugging
    >> Information" in the linker, then the file is bigger.

    > Ofcourse but debugging information are not usable by end-user


    However, it might be useful for the developer if it gets included in a bug
    report from an end user.
    Rolf Magnus, Feb 18, 2005
    #13
  14. [ ThrashDK ]

    Ingo Nolden Guest

    [ ThrashDK ] wrote:
    >>But the resulting .exe file is smaller
    >>in size with the new one, 73728 bytes.

    >
    >
    > I figured out, that this happens when you tick "Generate Debugging
    > Information" in the linker, then the file is bigger.
    >
    > And I have now tried it on two different PC's. It compiles with no errors on
    > both. I have had a look in the different settings, to see if something was
    > set incorrect, but I'm not sure. It doesn't appear to..
    >

    on my compiler VC++ from version 6.0 to 7.1 if you include

    #include <iostream.h>

    instead of

    #inlcude <iostream>

    you will get a version that is wrapped into std namespace.

    I don't know if this is releavent, but it has not been mentioned, and
    perhaps it may help somehow.

    the .h version is actually a pre-standard version as far as I know, as
    the iostream functions have been moved to the standard namespace either
    by the standard or by the compiler manufacturer.

    Ingo
    Ingo Nolden, Feb 19, 2005
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    551
  2. Kevin Spencer

    Re: Simple Simple question!!!

    Kevin Spencer, Jun 25, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    658
    Kevin Spencer
    Jun 25, 2004
  3. Daniel Frey

    Simple Question - Simple Answer?

    Daniel Frey, Dec 28, 2004, in forum: XML
    Replies:
    4
    Views:
    835
    Daniel Frey
    Jan 12, 2005
  4. Oli

    simple simple question

    Oli, Jan 26, 2004, in forum: ASP General
    Replies:
    10
    Views:
    367
    Roland Hall
    Jan 26, 2004
  5. Jerry C.
    Replies:
    8
    Views:
    229
    Uri Guttman
    Nov 23, 2003
Loading...

Share This Page