Can someone help with my first program ?

Discussion in 'Mac Programming' started by Recoverytwo, Feb 3, 2011.

  1. Recoverytwo macrumors newbie

    Joined:
    Feb 3, 2011
    #1
    I just started programing C++ and I am just trying to do the "Hello World" program and it wont compile (I am using Xcode), here is what I read to put in.


    #include <iostream>

    int main()
    {
    std::cout << "hellow world \n";
    return 0 ;
    }

    I get back "Iostream: no such file in directory" and "Expected expression before ":" token.

    Can someone please help ?
     
  2. balamw, Feb 3, 2011
    Last edited: Feb 3, 2011

    balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #2
    You may want to step back and learn how to use the compiler without Xcode first.

    Your code works fine, so it is something about the way you created your Xcode project that is causing the problem.

    EDIT: Borrowing from http://forums.macrumors.com/showthread.php?t=1078777

    Open Terminal.app

    Code:
    cd ~/Documents
    echo "#include <iostream>
    
    int main() 
    {
    std::cout << \"hellow world \n\";
    return 0 ; 
    }" > myHello.cpp
    g++ myHello.cpp
    ./a.out
    
    Works fine. Substitute an editor for the file creation via echo if you want something a bit easier to work with.

    B
     
  3. Hansr macrumors 6502a

    Joined:
    Apr 1, 2007
  4. Recoverytwo thread starter macrumors newbie

    Joined:
    Feb 3, 2011
    #4
    By George you've got it !

    I opened it up and used the objective-C class template in the iOS group, I didn't notice what i did before I started, is there any template I can use that will use C++ in iOS ?
     
  5. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #5
    If you are learning (and especially if you are learning C++ as your first language) then stick to Mac stuff for the time being. Writing iOS or Mac Cocoa apps that make use of C++ requires some knowledge of C++, Objective-C and Objective-C++. It is not going to help you.

    For the first few months most beginners just stick to writing command line applications, the GUI stuff will come later once you have started to feel comfortable with the programming language.
     
  6. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #6
    This does raise the question. If your goal is iOS development, why are you learning C++ first?

    You could at least be using Foundation classes in Objective-C to do the same thing as Kochan does in his book.

    Are you following a particular book or course?

    B
     
  7. Recoverytwo thread starter macrumors newbie

    Joined:
    Feb 3, 2011
    #7
    I am using "Sams Teach Yourself C++" I had tried to program C++ a few times before but got distracted by things.

    So you would suggest Objective C as a first programing language ?
     
  8. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    Why not? If your goal is to learn Objective-C and Cocoa Touch, learning another C-related, Object-Oriented language seems like a suboptimal approach. Learning is always good, and you can use C++ in Objective-C (called Objective-C++), so it's not terrible by any means, but you will need to learn Objective-C to interact with the frameworks needed to make iOS applications. Why not start there? I don't think C++ is easier to learn than Objective-C, so that's not a compelling reason to do C++ first.

    -Lee
     
  9. Blakeasd macrumors 6502a

    Joined:
    Dec 29, 2009
    #9
    IOS does not have any C++ templates. If you plan to develop for IOS then I would suggest learning Objective-C first.
     
  10. chrono1081 macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #10
    +1 for Objective-C first.

    C++ has a lot of gotchas and rotten things in it. It also takes forever to make something useful using it.
     
  11. Bill McEnaney macrumors 6502

    Joined:
    Apr 29, 2010
    #11
    That's partly why I refuse to program in it.
     

Share This Page