PDA

View Full Version : Please explain to me...




Soulstorm
Mar 23, 2005, 05:19 PM
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?



plinden
Mar 23, 2005, 05:35 PM
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?


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.

Mitthrawnuruodo
Mar 23, 2005, 05:36 PM
You need to include iostream.h to make cout available...

// 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... ;)

Soulstorm
Mar 23, 2005, 05:37 PM
so, when using "namespace", you don't have to write "std::" again in the file??

plinden
Mar 23, 2005, 05:42 PM
so, when using "namespace", you don't have to write "std::" again in the file??

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.

Mitthrawnuruodo
Mar 23, 2005, 05:43 PM
so, when using "namespace", you don't have to write "std::" again in the file??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... :)

plinden
Mar 23, 2005, 05:44 PM
You need to include iostream.h to make cout available...

// 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... ;)

Depends on whether you're learning C or C++. Why not learn about this straight off? Saves having to unlearn stuff later.

MacNeXT
Mar 23, 2005, 05:47 PM
so, when using "namespace", you don't have to write "std::" again in the file??

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.

Mitthrawnuruodo
Mar 23, 2005, 05:50 PM
Depends on whether you're learning C or C++. Why not learn about this straight off? Saves having to unlearn stuff later.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 (http://www.hib.no/english/AI/subjects/sod156.html) in English, does this look similar to the course you're taking, Soulstorm...?

Soulstorm
Mar 23, 2005, 05:50 PM
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!

plinden
Mar 23, 2005, 05:52 PM
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... ;)

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!"

plinden
Mar 23, 2005, 05:58 PM
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?


http://users.aber.ac.uk/auj/voidmain.shtml
http://homepages.tesco.net/~J.deBoynePollard/FGA/legality-of-void-main.html

Google is your friend.

Mitthrawnuruodo
Mar 23, 2005, 06:00 PM
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!"Not unusual, always a couple in every class, both exciting and a bit irritating at the same time... ;) :D

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!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...

Soulstorm
Mar 23, 2005, 06:06 PM
http://users.aber.ac.uk/auj/voidmain.shtml
http://homepages.tesco.net/~J.deBoynePollard/FGA/legality-of-void-main.html

Google is your friend.
That's good, but it is very technical for me. Anyway, I now know that the right thing is "int main()" and not "void main", although my book says so. So, I will use "int main()", since the XCode's compiler doesn't accept it otherwise, and then we will see...

Thanks, guys!

hcuar
Mar 23, 2005, 08:55 PM
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!

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.

hcuar
Mar 23, 2005, 08:56 PM
By the way... Your book seems like it might be a bit outdated.

Soulstorm
Mar 24, 2005, 01:31 AM
By the way... Your book seems like it might be a bit outdated.
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...