Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
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

macrumors 601
Apr 8, 2004
4,029
142
Soulstorm said:
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

Moderator emeritus
Mar 10, 2004
14,422
1,063
Bergen, Norway
You need to include iostream.h to make cout available...

Code:
// 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

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
so, when using "namespace", you don't have to write "std::" again in the file??
 

plinden

macrumors 601
Apr 8, 2004
4,029
142
Soulstorm said:
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

Moderator emeritus
Mar 10, 2004
14,422
1,063
Bergen, Norway
Soulstorm said:
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

macrumors 601
Apr 8, 2004
4,029
142
Mitthrawnuruodo said:
You need to include iostream.h to make cout available...

Code:
// 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

macrumors 6502
Jun 21, 2004
258
0
Soulstorm said:
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

Moderator emeritus
Mar 10, 2004
14,422
1,063
Bergen, Norway
plinden said:
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 in English, does this look similar to the course you're taking, Soulstorm...?
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
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

macrumors 601
Apr 8, 2004
4,029
142
Mitthrawnuruodo said:
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!"
 

Mitthrawnuruodo

Moderator emeritus
Mar 10, 2004
14,422
1,063
Bergen, Norway
plinden said:
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

Soulstorm said:
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

macrumors 68000
Original poster
Feb 1, 2005
1,887
1

hcuar

macrumors 65816
Jul 23, 2004
1,065
0
Dallas
Soulstorm said:
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.
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
hcuar said:
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...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.