-
This wont compile on Win2k, Win ME, Linux, FreeBSD- i keep getting the same error:
<source>
#include <iostream>
using namespace std;
int main () {
cout << "Hello world!\n";
return 0;
}
</source>
<error>
Undefined reference to "cout"
Undefined reference to "ostream::operator<<(char const *)"
-
Quote:
Originally posted here by HaLCy0n
The program that bludgeon posted will work, but I believe that is following the older standards. Let me explain exactly what the using namespace command does.
When you use the standard C++ functions, they are declared in the "std" scope. For example, if I did not put the "using namespace std;" I would have to say something like, "std::cout << "Hello World\n";" in order for it to work. That's just a little explanation.
If you wanna read more indepth about how namespaces work exactly, here's a link for you: http://www.cplusplus.com/doc/tutorial/tut5-2.html
Hope this helps.
HalCyOn, I was curious what "older standards" you were talking about in your post - respectfully, of course.
My code:
#include <iostream.h>
void main()
{
cout << "Hello World!" << endl;
}
....compiles and executes in MS Visual C++ 6 and . NET
I've used "using namespace std;" - just never in a program this simple. Thanks!
You guys may feel this way, too...but I feel in programming, though there are many solutions to a probelm, there is usually one shining, elegant, beautiful, and simple solution. You know....complex in it's simplicity, tidy, ......oh, I could go on....but I won't. :-)
Anyway, I'm not refering to "Hello World" - just programming in general, and I wondered if any of you felt the same way.
-
As cgkanchi said, this is the correct way to write this program:
#include <iostream>
using namespace std;
int main()
{
cout << "\n\nHello World!\n";
return 0;
}
or you could have written:
#include <iostream>
int main()
{
std::cout << "\n\nHello World!\n";
return 0;
}
The cout statement belongs in the std namespace and can only be used within this namespace.
As for the include directive '#include <iostream>', this is the correct directive. Some implementations of the Standard Template Library require the directive to look like this '#include <iostream.h>. Notice that the latter directive is platform dependent, and not the original implementation as proposed by the inventors of C++. Still in many IDEs the first is used, for instance Borland C++ and MS VC++. In GCC the original implementation is used.
One suggestion in this thread does this mistake:
#include <iostream.h>
void main(void)
{
cout << "Hello, World!";
}
Using 'void main(void)' is very non-standard and not the correct way to decalre the main function. It will work however with many compilers, but the good ones will not let this pass. GCC returns this error: 'hello.cpp:6: main must return int'. Somehow void main has become default in Borland (I think).
BTW there is no reason not to use C functions in C++. The big difference between the languages lies in C++'s use of objects. Using the Standard Template Library is not a big issue. But once you've learned it it's very useful.
-
Wow, I overlooked this one...you are using "gcc". That's the C compiler. g++ is what you are going to want.
If I'm wrong, someone please correct me, but I'm thinking this is the issue we all overlooked here.
-
dont you have to use g++ for cpp. i thought gcc was just for c code! i could be wrong but it would explain why the compiler didn't understand cout
sorry HaLCy0n your post wasnt there when i looked at the gif, but i agree with you.
-
Yes, you have to use the g++ command for c++, as the gcc command for c. GCC is also the name of the compiler though (GNU Compiler Collection). Confusing! GCC contains both the gcc and the g++ command.
-
Yes gcc is for C and g++ is for CPP. Although i am using DJGPP (in this case the compiler will know what style to link via the file ext. i may be wrong- if so i can't find the C++ compiler...) I am using the RHIDE development tool that is packaged with DJGPP. The reason i wanted to use DJGPP is so that i could use it for windows apps (DJGPP is GNU)... I compiled it on my FreeBSD Box, no problems... For any further information concerning DJGPP please goto http://www.delorie.com/djgpp/ , it's free! They also have a really neat tool:: a free public compiler (granted it's only in C and only a few libraries are available- it's still pretty cool> check it out == http://www.delorie.com/djgpp/compile/ )
-
gcc compile the code I posted just fine, no problems at all, if you try to use cin>> though you'll get a bunch of errors.
-
I tried it on the gcc compiler under cygwin, and got the same
error when invoking the compiler as gcc.
When I used the g++ command, it compiled perfectly, so
you have to use the right command for c++ code. It doesn't
automatically tell from the cpp extension.
:cool:
-
there is no g++ command with the DJGPP package, is it called something else... the DJGPP page isn't too informative...