Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 23, 2005, 05:19 PM   #1
Soulstorm
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
Location: Menidi, Athens, Greece
Please explain to me...

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?
__________________
Oramind: Articles, reviews Techlology and Art
If you don't like it, program it.
Soulstorm is offline   0 Reply With Quote
Old Mar 23, 2005, 05:35 PM   #2
plinden
macrumors 68040
 
plinden's Avatar
 
Join Date: Apr 2004
Quote:
Originally Posted by Soulstorm
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.
plinden is offline   0 Reply With Quote
Old Mar 23, 2005, 05:36 PM   #3
Mitthrawnuruodo
Demi-God (Moderator)
 
Mitthrawnuruodo's Avatar
 
Join Date: Mar 2004
Location: 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...
__________________
Those who fail to learn history are doomed to repeat it; those who fail to learn history correctly... why, they are simply doomed.

Last edited by Mitthrawnuruodo; Mar 23, 2005 at 05:39 PM.
Mitthrawnuruodo is offline   0 Reply With Quote
Old Mar 23, 2005, 05:37 PM   #4
Soulstorm
Thread Starter
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
Location: Menidi, Athens, Greece
so, when using "namespace", you don't have to write "std::" again in the file??
__________________
Oramind: Articles, reviews Techlology and Art
If you don't like it, program it.
Soulstorm is offline   0 Reply With Quote
Old Mar 23, 2005, 05:42 PM   #5
plinden
macrumors 68040
 
plinden's Avatar
 
Join Date: Apr 2004
Quote:
Originally Posted by Soulstorm
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.
plinden is offline   0 Reply With Quote
Old Mar 23, 2005, 05:43 PM   #6
Mitthrawnuruodo
Demi-God (Moderator)
 
Mitthrawnuruodo's Avatar
 
Join Date: Mar 2004
Location: Bergen, Norway
Quote:
Originally Posted by Soulstorm
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...
__________________
Those who fail to learn history are doomed to repeat it; those who fail to learn history correctly... why, they are simply doomed.
Mitthrawnuruodo is offline   0 Reply With Quote
Old Mar 23, 2005, 05:44 PM   #7
plinden
macrumors 68040
 
plinden's Avatar
 
Join Date: Apr 2004
Quote:
Originally Posted by Mitthrawnuruodo
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.
plinden is offline   0 Reply With Quote
Old Mar 23, 2005, 05:47 PM   #8
MacNeXT
macrumors 6502
 
Join Date: Jun 2004
Quote:
Originally Posted by Soulstorm
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.
__________________
Oh, and by the way, Micro$oft suxorz!!!
MacNeXT is offline   0 Reply With Quote
Old Mar 23, 2005, 05:50 PM   #9
Mitthrawnuruodo
Demi-God (Moderator)
 
Mitthrawnuruodo's Avatar
 
Join Date: Mar 2004
Location: Bergen, Norway
Quote:
Originally Posted by plinden
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...?
__________________
Those who fail to learn history are doomed to repeat it; those who fail to learn history correctly... why, they are simply doomed.

Last edited by Mitthrawnuruodo; Mar 23, 2005 at 05:55 PM.
Mitthrawnuruodo is offline   0 Reply With Quote
Old Mar 23, 2005, 05:50 PM   #10
Soulstorm
Thread Starter
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
Location: Menidi, Athens, Greece
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!
__________________
Oramind: Articles, reviews Techlology and Art
If you don't like it, program it.

Last edited by Soulstorm; Mar 23, 2005 at 05:52 PM.
Soulstorm is offline   0 Reply With Quote
Old Mar 23, 2005, 05:52 PM   #11
plinden
macrumors 68040
 
plinden's Avatar
 
Join Date: Apr 2004
Quote:
Originally Posted by Mitthrawnuruodo
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 is offline   0 Reply With Quote
Old Mar 23, 2005, 05:58 PM   #12
plinden
macrumors 68040
 
plinden's Avatar
 
Join Date: Apr 2004
Quote:
Originally Posted by Soulstorm
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.deBoyn...void-main.html

Google is your friend.
plinden is offline   0 Reply With Quote
Old Mar 23, 2005, 06:00 PM   #13
Mitthrawnuruodo
Demi-God (Moderator)
 
Mitthrawnuruodo's Avatar
 
Join Date: Mar 2004
Location: Bergen, Norway
Quote:
Originally Posted by plinden
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...

Quote:
Originally Posted by Soulstorm
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...
__________________
Those who fail to learn history are doomed to repeat it; those who fail to learn history correctly... why, they are simply doomed.
Mitthrawnuruodo is offline   0 Reply With Quote
Old Mar 23, 2005, 06:06 PM   #14
Soulstorm
Thread Starter
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
Location: Menidi, Athens, Greece
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!
__________________
Oramind: Articles, reviews Techlology and Art
If you don't like it, program it.
Soulstorm is offline   0 Reply With Quote
Old Mar 23, 2005, 08:55 PM   #15
hcuar
macrumors 65816
 
hcuar's Avatar
 
Join Date: Jul 2004
Location: Dallas
Quote:
Originally Posted by Soulstorm
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.
__________________
iMac 27" i5 Mid 2011 | 1TB HD | 16GB RAM
iPhone 4 32 Gig
hcuar is offline   0 Reply With Quote
Old Mar 23, 2005, 08:56 PM   #16
hcuar
macrumors 65816
 
hcuar's Avatar
 
Join Date: Jul 2004
Location: Dallas
By the way... Your book seems like it might be a bit outdated.
__________________
iMac 27" i5 Mid 2011 | 1TB HD | 16GB RAM
iPhone 4 32 Gig
hcuar is offline   0 Reply With Quote
Old Mar 24, 2005, 01:31 AM   #17
Soulstorm
Thread Starter
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
Location: Menidi, Athens, Greece
Quote:
Originally Posted by hcuar
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...
__________________
Oramind: Articles, reviews Techlology and Art
If you don't like it, program it.
Soulstorm is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Can someone please explain to me why they're referred to as GSM and CDMA... zorahk iPhone 34 Feb 15, 2011 08:47 AM
Mac Mini 2010 - Can someone please explain to me how to get 5.1 surround via HDMI? Baron Aloha Mac mini 12 Jul 16, 2010 04:40 AM
Could someone please explain to me what exactly I need to do? BaLaClavaAa Jailbreaks and iOS Hacks 5 May 1, 2009 06:28 PM
Please explain to me... No2 Soulstorm Mac Programming 12 Sep 9, 2005 11:04 AM
Please explain to me... mymemory General Mac Discussion 20 May 6, 2002 05:13 PM


All times are GMT -5. The time now is 03:55 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Privacy / DMCA contact / Affiliate and FTC Disclosure
Copyright 2002-2013, MacRumors.com, LLC