| View previous topic :: View next topic |
| Author |
Message |
youlikeicecream LXF regular
Joined: Fri Jun 03, 2005 12:40 pm Posts: 721 Location: Oxford
|
Posted: Wed Jun 22, 2005 5:54 pm Post subject: C++ probs |
|
|
Hi, bit new to C++ ... have written simple programs in C and compiled them on a remote UNIX shell using CC before but now when I try to compile programs on my Linuxbox at home It basically wont compile a damn thing. Here is the code I am using ....
***********************************************
#include <iostream.h>
main()
{
cout << "Hello World! ";
}
**********************************************
--- I type (as root) :
# gcc hello.c -o helloprog
--- and get these errors :
hello.c:1:22: iostream.h: No such file or directory
hello.c: In function `main':
hello.c:6: error: `cout' undeclared (first use in this function)
hello.c:6: error: (Each undeclared identifier is reported only once
hello.c:6: error: for each function it appears in.)
also I did a quick search for iostream.h and found it under /usr/include/c++/3.3/backward/iostream.h
but I still got the same error message that it could not be found!!
can anyone help? Am I doing something really stupid or is something improperly installed?
thanks
Mike  |
|
| Back to top |
|
 |
wiz LXF regular
Joined: Thu Apr 07, 2005 7:20 pm Posts: 119 Location: In front of a computer
|
Posted: Wed Jun 22, 2005 6:58 pm Post subject: |
|
|
Try using
g++ hello.c -o helloprog
I think you need the the c++ compiler. |
|
| Back to top |
|
 |
1slipperyfish Forum Jester

Joined: Mon May 09, 2005 3:52 pm Posts: 2366 Location: wigan
|
Posted: Wed Jun 22, 2005 7:37 pm Post subject: |
|
|
i used | Code: | #include <iostream>
main()
{
std::cout << "Hello World! ";
}
|
saved it as hello.cpp in /home/paul
then complied it as g++ -o hello hello.cpp (as root) and ran it with ./hello
i am no expert but the compiler you said you used at work is probably an older one as not all compilers will accept the ".h" part
plus you had missed off the std:: part the only way around that would be to put using namespace std; under #include....(but i wouldn't if you are new to c++) as this is older c++ as is .h
hope this helps
paul _________________ i am a follower of the culture

Last edited by 1slipperyfish on Wed Jun 22, 2005 7:50 pm; edited 3 times in total |
|
| Back to top |
|
 |
alloydog LXF regular

Joined: Thu Apr 07, 2005 8:32 pm Posts: 600
|
Posted: Wed Jun 22, 2005 7:43 pm Post subject: |
|
|
Also, try losing the .h from the iostream
I found this with g++. I got the message that the .h has been depreciated, but it still compiled, after I ignored the warning. Rerunning the compiler with just #include <iostream.h> worked fine with out any messages.
For C++, don't you need to save the inital file with the extention .cpp?
It seems nearly every "Hello World" tutorial is different! As one of the more recent one's followed gave:
| Code: | #include <iostream>
using namespace std;
int main()
{
cout << "Hello World! /n";
return 0;
} |
Then it's compiled with:
g++ hello.cpp -o hello |
|
| Back to top |
|
 |
youlikeicecream LXF regular
Joined: Fri Jun 03, 2005 12:40 pm Posts: 721 Location: Oxford
|
Posted: Thu Jun 23, 2005 11:30 am Post subject: |
|
|
I managed to get Kdevelop to compile a simple KDE application after installing a whole heap of stuff but I cannot get a single peice of my code to run.
Thanks for the info guys
Mike  |
|
| Back to top |
|
 |
1slipperyfish Forum Jester

Joined: Mon May 09, 2005 3:52 pm Posts: 2366 Location: wigan
|
Posted: Thu Jun 23, 2005 7:18 pm Post subject: |
|
|
| Quote: | hello.c:1:22: iostream.h: No such file or directory
hello.c: In function `main':
hello.c:6: error: `cout' undeclared (first use in this function)
hello.c:6: error: (Each undeclared identifier is reported only once
hello.c:6: error: for each function it appears in.) |
as far as i know the line 1:22 is because as i said it is an older version of c++ that was a crossover from c
the other errors are for the line | Code: | | cout << "Hello World! "; | as you have not used | Code: | | std::cout <<"paul is great"; | that is all your errors mean (as they are all on line 6) another tip to avoid compiler errors is to press enter after your last bracket before you save it, otherwise you will get sometihng like " no newline at file end" or words to that effect
hope this helps
paul _________________ i am a follower of the culture
 |
|
| Back to top |
|
 |
otrus
Joined: Mon Jan 02, 2006 1:11 am Posts: 5 Location: Norway
|
Posted: Mon Jan 02, 2006 1:48 am Post subject: |
|
|
I had a similar problem. When I compiled using 'gcc -o hello hello.cpp' it returned a whole lot of errors. However, when I used 'g++ -o hello hello.cpp' it worked perfectly well. I tried using the '-x c++' option to specify c++ as the language, but that didn't work either. After a bit of research I found out that gcc didn't link to the c++ library, and even thogh I tried to explicitly specify the library I never got it to work. I probably didn't install the library properly.
Anyway, that's not a big problem since I can just use 'g++' to make everything work. |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|