XCode compiler for mac and books

Pages: 12
So for the remainder of the book, I just have to change everything into the iostream (from the std_lib_facilities.h? Is that all? But even after I corrected that, there is still an error.
IF you have errors, post them here. Then we can help.

Have a skim read of the book, see where he stops talking about std_lib_facilities.h I say again it only for a short time not the whole book.

For now using <iostream> , but if he starts talking about String or Vector or List for example, then you need to include those files and refer to them correctly in the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <vector>
#include <list>

int main() {
    std::string MyString = "Me";
    std::vector<int> MyVector = {1, 2,3 ,4 ,5};
    std::list<std::string> MyList {"A", "List", "of", "words"} ;

    std::cout << "MyString is " << MyString << "\n";
    // printing out the others is too advanced right now

    // std::cout << "Program Successful - ending ";
return 0;
}
Last edited on
I get that, but even if I change it to iosteam, there's still an error.

in short, apart from changing the std_lib_facilities.h to iostream, what else should I change from the code given in his book?

It seems like that I have to put std::cout. What other irregularities are there?
I get that, but even if I change it to iosteam, there's still an error.


What is the error - I will explain it.

You can run code on this page by clicking on the gear icon on the right of the code. I might have made a typo missing semicolon say.

in short, apart from changing the std_lib_facilities.h to iostream, what else should I change from the code given in his book?


TheIdeasMan wrote:
For now using <iostream> , but if he starts talking about String or Vector or List for example, then you need to include those files and refer to them correctly in the code:


It seems like that I have to put std::cout. What other irregularities are there?


One has to put std:: before each thing that belongs in the std namespace. Notice I had them to refer to the types std::vector, std::string and std::list

The other thing to do, is read the documentation for whatever it is that you are using - it will tell what file you need to include and in what namespace it is from - nearly always std::

The purpose of namespaces is to separate code so there are no naming conflicts. If we both put our code into their own namespaces, then we can both have std::string MyString; Mine might be referred to as TIM::MyString , while yours might be WFE::MyString

http://www.cplusplus.com/reference/vector/vector/
Hi,

You do know that you can paste your code into a reply, select it, then format it as code with the <> button of the format menu on the right?

I ran (directly on this page using the gear icon) both snippets of code I posted. I first one was fine, the second had a missing semicolon on line 9, that is fixed now.

So if you had a problem with the first one, there must some typo in your code. Did you spell iostream properly ?

Anyway hopefully we can get to the point where you can run your "Hello World!" program successfully. Hopefully soon - I need to go to bed soon :+)
it seems as if that the book forgot to put std:: before cout. is that just a typo on the side of the book? The book was published at 2008 - maybe I'm using a newer compiler? I just downloaded it off a site - it's probably illegal. I'll try to find a legal copy of a newer book first. But I'll first do the tutorial and ask here if I have questions
Last edited on
The book probably has "using namespace std;" in it somewhere or implies it.
Edit: Here http://www.stroustrup.com/Programming/std_lib_facilities.h a few lines down
Last edited on
No - a lot of authors do things to make it easier for beginners, Stroustrup has a declaration inside his header file which means that a beginner can leave out the std:: part.

Other authors put a using namespace std; before main, so beginners can avoid the need for all of the instances of std::. It sounds good and easy, but it is actually really bad - it brings in the entire std namespace which has heaps of stuff in it (the entire STL) . This can cause naming conflicts - which negates the entire purpose of having a namespace in the first place.

I am really peeved that lots of authors do this - I wish they would just do it properly from the start. Sorry for all the confusion so early in your C++ experience, but you should be right now.

Anyway how is your Hello World Program going ? Does it build properly now?

If I don't reply, it's because I have gone to bed :+)
The only hello world program that I was able to run is the one from the tutorial, and the one from a book called "A complete Guide to Programming in C++", and it uses the namespace std. I copied programs from it, and it works.

Although as a personal good news, I currently have a teacher who's willing to teach me, but after I learn the basics of C++. So probably the tutorial is enough.
Hi,

This should always work, try it with the gear icon, or copy / paste it (don't type it in) into a new file in XCode:

1
2
3
4
5
6
7
8
#include <iostream>

int main() {

    std::cout << "Hello World!\n";

   return 0;
}


If it works here, but not on your system - then something is going on in your system, or there is something you have done. If that is the case, post a screen shot again.

Crikey, it sounds as though you have lots of books :+) I guess you could go with whatever feels most comfortable for you. I have just been trying to point out best practices for you - right from the start. Although I guess I was doing that in the face of some very common though slightly bad conventions found in textbooks.

Edit:

I mean the following in the nicest most sincere way - so I hope you don't see it as negative criticism.

The other thing is, if something doesn't work then post the code and any errors here, and / or a screen-shot.

Prefer to do this, rather than say "It doesn't work" , or "the only one that worked had this"

It sounds as though you couldn't get anything I gave you to work, or what I was saying was wrong, and I haven't achieved anything at all through all of that correspondence through a large part of yesterday evening. This is a little frustrating from my point of view, because I know what I gave you does work 100%.

Don't worry I am not really upset or anything - much worse things have happened in my life - Ha ! :+)

Any way, Good Luck , we are all still here if you have any more questions.
Last edited on
Topic archived. No new replies allowed.
Pages: 12