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

tyskindian

macrumors newbie
Original poster
May 10, 2009
3
0
I started learning c++ many years ago in windows and now wants to recap that in OS X.

This code:

Code:
#include <iostream>

int main();
{
	cout << "Hello World!\n";
	return 0;

}

Generates the following error:

Code:
hello.cpp:4: error: expected unqualified-id before ‘{’ token

Whats wrong?

And BTW. I tried first to include iostream.h but the compiler then warned that it is "decrepated". Should I no longer, for any cpp header, use ".h"?

Thanks for all help!
 
A couple of problems.

Code:
#include <iostream>

int main() //No semicolon needed here.
{
	[b]std::[/b]cout << "Hello World!\n";
	return 0;
}

And BTW. I tried first to include iostream.h but the compiler then warned that it is "decrepated". Should I no longer, for any cpp header, use ".h"?

Some but not all. Headers like iostream and cstdlib don't have .h, but if you include C headers then you need the .h.
 
Thanks, but... just removing the semicolon makes no difference and if I before "cout" add "std::" I get the following from the compiler instead :confused::

Code:
Undefined symbols:
  "___gxx_personality_v0", referenced from:
      ___gxx_personality_v0$non_lazy_ptr in cceRTNkh.o
  "std::ios_base::Init::~Init()", referenced from:
      ___tcf_0 in cceRTNkh.o
  "std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
      _main in cceRTNkh.o
  "std::ios_base::Init::Init()", referenced from:
      __static_initialization_and_destruction_0(int, int)in cceRTNkh.o
  "std::cout", referenced from:
      __ZSt4cout$non_lazy_ptr in cceRTNkh.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
 
Code:
#include <iostream>

int main () {
[INDENT]std::cout << "Hello World!" << std::endl;
return 0;[/INDENT]

}

The culprit is the '\n' whitespace char. C used this, but C++ uses 'std::endl'.
 
I just did the compile of the following:

cppTest.C

Code:
#include <iostream>

int main(int argc, char *argv[])
{
	std::cout << "Hello World!\n";
	return 0;

}

using

g++ cppTest.C -o cppTest

it worked just fine...

Did you install the Developer tools or did you use the built in compiler?
 
Code:
#include <iostream>

int main () {
[INDENT]std::cout << "Hello World!" << std::endl;
return 0;[/INDENT]

}

The culprit is the '\n' whitespace char. C used this, but C++ uses 'std::endl'.

The "\n" is still valid in C++.

You can even get rid of the std:: by doing the following:

Code:
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
	cout << "Hello World!\n";
	return 0;

}
 
Code:
#include <iostream>

int main () {
[INDENT]std::cout << "Hello World!" << std::endl;
return 0;[/INDENT]

}

The culprit is the '\n' whitespace char. C used this, but C++ uses 'std::endl'.
This made no difference.

I just did the compile of the following:

cppTest.C

Code:
#include <iostream>

int main(int argc, char *argv[])
{
	std::cout << "Hello World!\n";
	return 0;

}

using

g++ cppTest.C -o cppTest

it worked just fine...

Did you install the Developer tools or did you use the built in compiler?
This worked just fine - thanks! But what does "int argc, char *argv[]" mean? And yes, I did install the developer tools.

The "\n" is still valid in C++.

You can even get rid of the std:: by doing the following:

Code:
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
	cout << "Hello World!\n";
	return 0;

}

Sweet! Thansk again, lets see how far I can make it on my own now...:)
 
This worked just fine - thanks! But what does "int argc, char *argv[]" mean?

Those are the arguments passed on the command line to the program.

If you've ever done any terminal commands, ie.

cp sourcefile destinationfile

The sourcefile and destinationfile are both arguments to the program named cp.

You will see that the argc counter is 2 and the argv * contains the strings for sourcefile and destinationfile.

depending on the OS, the arguments would come out like this:

argv[0] = "cp" -- this is the program name
argv[1] = "sourcefile"
argv[2] = "destinationfile"
 
Righty-o.

Code:
#include <iostream>

int main () {
[indent]std::cout << "Hello World!" << std::endl;
return 0;[/indent]
}

Is working code...you don't need the extra command line input argument declarations for the main.

So the code above using
Code:
g++ -o hello hello.cc
doesn't work on your machine?
 
The real problem is that you were compiling your C++ program with a C compiler (that's where this ___gxx_personality_v0 stuff came from). GCC looks at the extension of your program to decide whether it's C (.c) or C++ (.cpp or .cc, or maybe .C). Of course, you can use "g++" instead of "gcc" to compile your program as well.

Like sammich said, the argc/argv stuff has nothing to do with your problem and you can safely use int main() for your program.

Good luck!
 
Well im not sure about your compiler but when i made a hello world thing i used this code:

Code:
#include <iostream>
using namespace std;

int main()
{
        cout << "Hello World!\n";
    
        return 0;
}
 
Thanks

I was getting funky output as well when trying to use gcc. at first, i thought it was a problem with using TextEdit.

So, i switched to vi, got a different set of problems.

After switching to g++ instead of gcc, everything is working fine now...

only 23 more hours to go in the SAMS book. :D
 
Dont feel bad OP. I cant get hello world to work either in XCode (I think your using xcode) but my SDL programs work just fine.
 
Code:
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
	cout << "Hello World!\n";
	return 0;

}

It's a bad habit, when the project get large and many namespace get involved. But it's shorter for small project.

You should learn to use a makefile pretty quickly if you don't want to do this every time. Here's a good link for the basic stuff:
http://www.gnu.org/software/make/manual/make.html
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.