Please explain to me...

Discussion in 'Mac Programming' started by Soulstorm, Mar 23, 2005.

  1. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #1
    I just started my university lessons. So, they gave us a book, which explains how are we going to write in c++. The book was written to use a c++ compiler in an PC that runs the free compiler named DevC++ (from Bloodshed software).

    But I want to use XCode, so that I don't have to use Virtual PC!

    So i open xCode and I make a new project (c++ tool) and then I follow my book's instructions which gives me an example hello world application:

    int main()
    {
    cout << "Hello world";
    }

    But XCode gets me an error "cout undeclared"!!!
    It wants me to write "std::cout" instead! What is going on? Perhaps I am doing something wrong?

    I just want to program simple console applications using C++ on a mac. How can I do that?
     
  2. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #2

    Don't know about either XCode or DevC++, but to understand this you should read about namespaces in your book. This should be written in one of two ways:
    #include <iostream>

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


    or:
    #include <iostream>
    using namespace std;

    int main()
    {
    cout << "Hello world";
    }


    I'm personally very much against using the second one - it saves typing but makes for lazy programming. I prefer specifying the namespace explicitly, but that's just my preference - whatever you do /never/ use "using namespace" in a header file.
     
  3. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #3
    You need to include iostream.h to make cout available...

    Code:
    // Hello world program
    #include <iostream.h>
    
    int main()
         {
              cout << "Hello world";
          }
    EDIT: Damn, beaten to it... ;)

    EDIT2: Still, "my" simpler example should work just fine, I think namespaces comes much later... ;)
     
  4. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #4
    so, when using "namespace", you don't have to write "std::" again in the file??
     
  5. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #5
    Correct.

    I just looked up Dev-C++ and it's just an IDE that wraps gcc (the GNU C compiler). gcc is also available for OS X, and it's possible that Dev-C++ might work too in an X11 environment on a Mac, but I'll leave it up to someone with a Mac to confirm or deny that.

    Anyway, if the original program is how it's presented in the book, I'm not very impressed.
     
  6. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #6
    The namespace is just if you use new-style headers, like <iostream>, and not the good old <iostream.h>... start with the basics, and inlcude <iostream.h> and forget namespaces untill your teacher mentiones them... :)
     
  7. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #7
    Depends on whether you're learning C or C++. Why not learn about this straight off? Saves having to unlearn stuff later.
     
  8. macrumors 6502

    Joined:
    Jun 21, 2004
    #8
    Just use "std::". Namespace might seem like a convenient solution for small programs, but you'll appreciate "std::" when you get to create bigger things. Take the chance to get used to it.
     
  9. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #9
    Well, as a former C/C++ intro course teacher, I soon found out that the simpler you make things, in the beginning, the more students actually can learn the basics, and then later, some of them, start asking "strange" questions, then you explains some more sophisticated or new methods... ;)

    Edit: Found a link to the course in English, does this look similar to the course you're taking, Soulstorm...?
     
  10. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #10
    Guys, the example program in the book does not mention anything about namespaces. It is here that I found this info. However, in my book the program is written

    #include <iostream.h>
    void main()
    {
    cout << "hello"
    }

    I don't see anything about any namespaces. Another thing I want to ask... Why in the book the main is written "void main()", but in the Mac OS X gcc compiler it is necessary to writte it like "int main()"?? What's the difference?

    Forgive me, but I am new to C and C++ and I need some info, because the univerities here at greece are completely useless!

    EDIT: It seems the DevC++ application makes a new file and has namespaces enabled by default!
     
  11. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #11
    Hmm, ok, but that kind of thing always frustrated me - I would be two or three chapters ahead of the course in the book and would be asking myself "why didn't anyone tell us this earlier? That makes it so much easier to understand!"
     
  12. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #12
  13. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #13
    Not unusual, always a couple in every class, both exciting and a bit irritating at the same time... ;) :D

    void and int will be explained in due time... but you may start using the "int main()", and include a "return 0;" as the last line in the main method. That is the ISO way to go...

    The int indicates that the method will return an int when it's finished...
     
  14. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #14
  15. macrumors 65816

    hcuar

    Joined:
    Jul 23, 2004
    Location:
    Dallas
    #15
    iostream.h is an old header that doesn't require namespaces (it was created before namespaces existed). iostream (note it doesn't have the .h) requires the use of namespaces and is the new implementation of i/o streams.

    Ansi standard C++ requires that the main function return an integer. The integer defines the exit status for the application. The pre Ansi standard C++ had no such requirement. xCode is actually using the same compiler as Bloodshed Dev C++. (GCC, although it could be mingw a variation of GCC IIRC) ;) So... My recommendation as a Software Design Engineer is to learn the Ansi C++ is possible.
     
  16. macrumors 65816

    hcuar

    Joined:
    Jul 23, 2004
    Location:
    Dallas
    #16
    By the way... Your book seems like it might be a bit outdated.
     
  17. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #17
    I know. I hate it and I am thinking of buying a second one... I will study the one they gave us at the university, but I will try to see the changes that happened after mmy book was written, using the one I will buy...
     

Share This Page