Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Xcode: C++ program stops in the middle of the code

Hello fellow C++ developers, I'm a novice in C++ programming. This is my code:


//

// main.cpp

// Test

//

// Created by (USERNAME) on 5/19/13.

// Copyright (c) 2013 (USERNAME). All rights reserved.

//


#include <iostream>


int main(int argc, const char * argv[])

{

usingnamespace std;

cout << "Hello world!" << endl;

cout << "Enter a word: ";

string word = "";

cin >> word;

cout << "You entered: \"" << word << "\"" << endl;

string name = "";

cout << "Enter your name: ";

cin >> name;

cout << "Hello, " << name << ", my name is Computer." << endl;

cout << "I am a C++ program" << endl; //Program terminates here

string worda = "";

cout << "Enter another word: ";

cin >> worda;

cout << "You entered: \"" << worda << "\"" << endl;

return 0;

}


Does anyone understand why this is (the program terminates there)?

Posted on May 19, 2013 3:38 AM

Reply
12 replies

May 19, 2013 6:14 AM in response to Arc676

You have to declare the method, with just the signature, first in a place where it will be visible to the code calling it. Then you can define the method pretty much anywhere and the linker will connect it all together.


Also, I don't think you have any classes defined. A method is an action attached to a class. If there is no class, it is just a function.

May 19, 2013 6:19 PM in response to Arc676

The problem is that when you call foo();, the compiler doesn't know it exists yet. You have to declare it with:

void foo(void);

Then, the compiler will know that is a function. When it sees that function being called, it will stick a placeholder for it. Then it will see a function and compile that to object code right next to the main() function. Finally, the linker will go through and connect all of those function placeholders into actual function calls.

May 20, 2013 8:51 AM in response to Arc676

Arc676 wrote:


You mentioned declaring a class. Does this affect the declaration of functions in any way? How does declaring a class in C++ affect the program?

The forums are being flaky today. I just figured out how to free up the works.


In C++, a class virtually identical to a struct. The only difference is that, in a struct, all members are public by default whereas they are private by default in a class. Internally, you can imagine that class/struct methods are just regular old functions whose first parameter is a pointer to the class/struct itself. Well, you don't have to imagine that because that is exactly how it works. There is also a little bit of vtable magic that redirects the function call to the appropriate derived class/struct.


Java just does two passes through the file so that function declarations aren't necessary. C++ compilers typically don't do that because C++ has traditionally been very difficult and time consuming to compile. Apple has fixed most of those speed problems in GCC with its Clang compiler. Consequently, in Objective-C you don't need to forward declare all methods as you do in C++.

Xcode: C++ program stops in the middle of the code

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.