C++ probs

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

C++ probs

Postby youlikeicecream » Wed Jun 22, 2005 4:54 pm

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 :)
youlikeicecream
LXF regular
 
Posts: 721
Joined: Fri Jun 03, 2005 11:40 am
Location: Oxford

Postby wiz » Wed Jun 22, 2005 5:58 pm

Try using

g++ hello.c -o helloprog

I think you need the the c++ compiler.
wiz
LXF regular
 
Posts: 119
Joined: Thu Apr 07, 2005 6:20 pm
Location: In front of a computer

Postby 1slipperyfish » Wed Jun 22, 2005 6:37 pm

i used
Code: Select all
#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
Last edited by 1slipperyfish on Wed Jun 22, 2005 6:50 pm, edited 3 times in total.
i am a follower of the culture

Image
User avatar
1slipperyfish
Forum Jester
 
Posts: 2432
Joined: Mon May 09, 2005 2:52 pm
Location: wigan

Postby alloydog » Wed Jun 22, 2005 6:43 pm

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: Select all
#include <iostream>

using namespace std;

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


Then it's compiled with:
g++ hello.cpp -o hello
User avatar
alloydog
LXF regular
 
Posts: 600
Joined: Thu Apr 07, 2005 7:32 pm

Postby youlikeicecream » Thu Jun 23, 2005 10:30 am

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 :)
youlikeicecream
LXF regular
 
Posts: 721
Joined: Fri Jun 03, 2005 11:40 am
Location: Oxford

Postby 1slipperyfish » Thu Jun 23, 2005 6:18 pm

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: Select all
 cout << "Hello World! ";
as you have not used
Code: Select all
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

Image
User avatar
1slipperyfish
Forum Jester
 
Posts: 2432
Joined: Mon May 09, 2005 2:52 pm
Location: wigan

Postby otrus » Mon Jan 02, 2006 1:48 am

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.
otrus
 
Posts: 5
Joined: Mon Jan 02, 2006 1:11 am
Location: Norway


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest