C++ and XCode help

Discussion in 'Mac Programming' started by Watabou, Aug 10, 2008.

  1. Watabou macrumors 68040

    Watabou

    Joined:
    Feb 10, 2008
    Location:
    United States
    #1
    I'm beginning my c++ programming.

    So I'm kinda of a newbie. I learned java if that helps.
    I'm a beginner so please help me as I'm really new to programming c++. Basically, I don't know anything. So I hope someone can help me here.
    I looked for a compiler for c++ and tried both eclipse and XCode. I don't know how to use any of them :(

    I used eclipse for java programming in class and this was easy to use. So thinking that, I installed both XCode and eclipse to give them both a try.

    Tried XCode but that seemed unfamiliar and I Started off as New-> Project and when I selected, C++ Carbon, it included bunch of Mac specific files.

    So I tried eclipse. Whenever I create a new project, it creates two classes for one class I create. For example, I created HelloWorld class and it created two classes for me: "HelloWorld.cpp" and "HelloWorld.h" What am I supposed to do with those?

    I typed this code in the cpp class and then ran it and it gave me an error saying no binary found. What does that mean? THis is the code:

    Code:
    #include "Code.h"
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
    cout<<"Hello World!\n";
    cin.get();
    }
    
    And in Eclipse, when I created the class, in the cpp class this code was already inserted by eclipse :

    Code:
    #include "HelloWorld.h"
    
    HelloWorld:: HelloWorld() {
    	// TODO Auto-generated constructor stub
    
    }
    
    HelloWorld::~ HelloWorld() {
    	// TODO Auto-generated destructor stub
    }
    
    And this was included in the HelloWorld.h file it already created for me:

    Code:
    #ifndef HELLOWORLD_H_
    #define HELLOWORLD_H_
    
    class HelloWorld {
    public:
    	HelloWorld();
    	virtual ~HelloWorld();
    };
    
    #endif /* HELLOWORLD_H_ */
    
    
    If someone can help me how to compile in either eclipse or XCode, I will be very grateful. Remember, I'm just starting out.
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    I modified the code you posted very slightly, removing the include that I didn't see a need for. I saved this file as hello.cpp. After that, from the terminal, I ran:
    g++ -o hello hello.cpp
    and ran the program it generated with:
    ./hello
    I got the output:
    Hello World!

    And the program waited for a line of input then exited (with an implicit return of 0).

    At this level, using an IDE is overkill. Use a text editor and g++ to compile and the terminal to run programs. You'll get a better feel for what's going on.

    -Lee
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    Sorry, double post.
    Wanted to take it a step further, here's a version that uses a class, some std::strings, etc.:

    TestHello.cpp
    Code:
    #include <iostream>
    #include "HelloWorld.h"
    using namespace std;
    
    int main(int argc, char *argv[]) {
      HelloWorld myHello;
      myHello.display();
      cin.get();
      return 0;
    }
    
    HelloWorld.h
    Code:
    #ifndef HELLOWORLD_H_
    #define HELLOWORLD_H_
    #include <string>
    class HelloWorld {
    public:
            HelloWorld();
            ~HelloWorld();
            void display();
    private:
             std::string ToWho;
    };
    
    #endif /* HELLOWORLD_H_ */
    
    HelloWorld.cpp
    Code:
    #include "HelloWorld.h"
    #include <iostream>
    
    HelloWorld:: HelloWorld() {
      ToWho = "World";
    }
    
    HelloWorld::~ HelloWorld() {
      ToWho.clear();
    }
    
    void HelloWorld::display() {
      std::cout << "Hello, " << ToWho << "!" << std::endl;
    }
    
    Compiled with:
    g++ -o TestHello TestHello.cpp HelloWorld.cpp

    and run with:
    ./TestHello

    The result is:
    Hello, World!

    And the program waits for a line of input, then exits with an explicit return code of 0. Any C++ experts might have advice on better practices, etc... i'm not much of a C++ programmer.

    -Lee

    Edit: Doing some reading, apparently "using namespace" in .h is frowned upon. Removed this.
     
  4. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #4
    Hi Lee,

    In your example code, the constructor is best written using an initialisation list, ie

    Code:
    HelloWorld:: HelloWorld()
    
       :   ToWho( "World" )
    
    {
    }
    
    In this way, ToWho uses a constructor that initialises the string. This is better and faster I think than using the default string constructor and then assigning a value in the body of the HelloWorld constructor (which is what your constructor was doing).

    Also the HelloWorld destructor will automatically call the destructor for member variables, so there is no need to clear ToWho first, ie the destructor should simply be:-

    Code:
    HelloWorld::~ HelloWorld()
    {
    }
    
    (as a side note, since, in this case , the destructor is empty, there is no need to declare or implement it).

    RAII is the fancy term for the way C++ manages resource allocation and release. Memory management in C++ is much easier in my opinion than Obejctive-C!

    b e n
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    Good times. At least I managed to write something that compiled, ran properly, and didn't leak, even if it was overly verbose and sub-optimal =). I'm glad someone was able to point out the right way to do this.

    Protip for beginners: You can even mess up Hello World. =)

    -Lee
     
  6. Watabou thread starter macrumors 68040

    Watabou

    Joined:
    Feb 10, 2008
    Location:
    United States
    #6
    Ok Thanks for helping but where did you write the code? Text Edit or XCode? I don't even see a editor there. Sorry for being so stupid but I really can't see how to bring the editor in Xcode.

    Edit: OK, So I got Xcode to show the editor when I created an empty project and then created a c++ file. Then I wrote the code and saved the file as hello.cpp to my desktop. How do I access my desktop through the terminal again> Wasn't it some cd/desktop or something like that? I can't find it on google.


    EDIT2: Success! I got the result. I saved the file to my desktop right? I accessed my desktop through the terminal via this command: cd ~/Desktop

    and then ran the g++ -o hello hello.cpp

    and then double clicked the Executable file it created on the desktop and saw Hello World there.

    Although I don't know what the ./hello you were talking about is. But thanks lee and lazydog.

    Ok So now what I wanted to know is what the hell is the .cpp and .h after the file names? And How do I save the .h file? Same way as the .cpp?

    Is the .cpp like a main class like how Java's is? In Java, I know that you can have multiple classes and one main class that has the "public static void main()" function.
     
  7. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #7
    Watabou

    If you want to use Xcode, then start a new project, and select C++ command line tool. In the project tree on the left, look around and find the main.cpp file. It will be in a folder called src. You can bring the editor window up either by double clicking on a source file, selecting the menu item View->Zoom Editor In, or using the key combination shift command E.

    Lee, your example code was fine and very useful for a newbie. I've always admired the amount of time and energy you put into helping people on this forum.

    b e n
     
  8. Watabou thread starter macrumors 68040

    Watabou

    Joined:
    Feb 10, 2008
    Location:
    United States
    #8
    Hmm, when I create a new project, I'm met with this window and it only has the Carbon C++ and Carbon C++ Standard applications. I just create a empty project right?

    Edit: Lazydog, thanks so much, I just had to scroll down to command line and expand it. It worked. Thanks!

    P.S. Now the .cpp and .h help please :) thanks.
     

    Attached Files:

  9. Sam Yikin macrumors regular

    Joined:
    Oct 28, 2007
    #9
    Command Line Utility->C++ Tool.
     

    Attached Files:

  10. Watabou thread starter macrumors 68040

    Watabou

    Joined:
    Feb 10, 2008
    Location:
    United States
    #10
    Yep I found it before in my previous post. Thanks anyways though lol

    Now all I want help is on what .cpp and .h is.
    As I also said in my previous post:

     
  11. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #11
    cpp stands for (c) (p)lus (p)lus. It's to indicate the type of source file, as .c is generally a C-only source file. From the gcc man page:
    It also lists .ii as a c++ source file that will not go through the pre-processor.

    A .h file is a header file. This name is shared with C. I have seen .hpp as well. Since the compiler does not deal with these files directly, and instead their text is pulled in to other source files by means of #include statements, their extension is for human-recognizability, and not for the compiler's benefit.

    The .cpp files will contain your source code, function implementations, etc. The .h files will contain things that need to be shared amongst source files such as struct definitions, function prototypes, class declarations, some #define declarations, etc. You could get away with never having a .h file, but you would have to duplicate things in every source file you wanted to use a particular class, function, etc. in so the compiler would know the types of the functions you were using, etc.

    .cpp files and .h files are both just plain text. You should be able to save them in the same manner.

    Code:
    int main(int argc, char *argv[])
    Is the same as java's
    Code:
    public static void main(String[] args)
    This is the program's entry point. Each .cpp does not contain this function, only one per program. This is the same as java (you can have lots of classes defined, but you only have one class that has a main entry point per program. C and C++ are a little more free-form than java, so you may have helper functions in their own files, in the same file as main, etc.

    -Lee
     
  12. Watabou thread starter macrumors 68040

    Watabou

    Joined:
    Feb 10, 2008
    Location:
    United States
    #12
    Thank you very much lee. I really appreciate the help. I will keep everything you said in my mind.

    Thank you, again. :D
     

Share This Page