LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-10-2006, 08:53 PM   #1
cornbeef
LQ Newbie
 
Registered: Jan 2006
Location: nyc
Distribution: Slackware 10.2
Posts: 20

Rep: Reputation: 0
Question #include "string.h" is not working?


hello all

its my 4th day into learning linux (basic commands, how the OS works, creating users and directories etc.) on slackware 10.2 - i decided to try making sample programs. i used the one from the tutorial from this website "C++ programming in linux." its started off fine with the basic hello world program.

#include <iostream.h>
int main()
{
cout << "hello world";
return(0);
}

typed g++ hello.cpp
it created a file a.out*
ran the file a.out - it worked
so i decided to take it a step further and include the string class.
this is the next program

#include <iostream.h>
#include "string.h"

int main()
{
cout << "\n\n enter your name: ";
cin >> name;
cout << "\n your name is: " << name << "\n\n";
return(0);
}

file name never changed still called hello.cpp and i removed the old a.out*
and tried to recompile this new file g++ hello.cpp
this is exactly what i got (forgive me if i type something wrong)


madcow@darkstar:~/myprogs$ g++ hello.cpp
in file included from /usr/include/c++/3.3.6/backward/iostream.h:31,
from hello.cpp:1:
/usr/include/c++/3.3.6/backward/backward_warning.h:32:2: #warning this file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> eader for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warnin use -Wno-deprecated.
hello.cpp: In function 'int main()':
hello.cpp:6: error 'string' undeclared (first use this function)
hello.cpp:6: error (each undeclared identifier is reported only once for each fucntion it appears in)
hello.cpp:6: error syntax error before ':' token
hello.cpp:9: error 'name' undeclared (first use this function)
madcow@darkstar:~/myprogs$


it never compiled obviously because of the errors. this works fine on my borland compiler for windows. my next step was to look through the paths that the warnings gave me. through the usr/include/ i seen all the .h files there, i found string.h there but was unable to find the iostream.h in the same folder. i found iostream without the .h in the folder /usr/include/c++/3.3.6/ im still trying to understand everything - why are the header libraries in different sections? and how come iostream is without the .h? and how come my program wont compile?
also on the errors, it said to consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard - how do i look at the particular file?

forgive me but im completly new to linux trying to learn on my own - thats why im posting, any info would be great



***He who dares not grab the thorns, should never crave the rose***
 
Old 01-10-2006, 08:56 PM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
try using
Code:
#include <string>
instead
 
Old 01-10-2006, 09:02 PM   #3
cornbeef
LQ Newbie
 
Registered: Jan 2006
Location: nyc
Distribution: Slackware 10.2
Posts: 20

Original Poster
Rep: Reputation: 0
Unhappy

just tried this for the headers

#include <iostream.h>
#include <string>

...


got the same error- anything else to try?
 
Old 01-10-2006, 09:04 PM   #4
Cirdan
Member
 
Registered: Nov 2005
Distribution: Arch Linux
Posts: 84

Rep: Reputation: 15
#include <iostream>
#include <string.h>

iostream.h is deprecated(sp?).
 
Old 01-10-2006, 09:06 PM   #5
cornbeef
LQ Newbie
 
Registered: Jan 2006
Location: nyc
Distribution: Slackware 10.2
Posts: 20

Original Poster
Rep: Reputation: 0
#include <iostream>
#include <string>

same error =(
 
Old 01-10-2006, 09:09 PM   #6
Cirdan
Member
 
Registered: Nov 2005
Distribution: Arch Linux
Posts: 84

Rep: Reputation: 15
add .h to <string>

iostream is the only one you don't need .h in it.
 
Old 01-10-2006, 09:15 PM   #7
cornbeef
LQ Newbie
 
Registered: Jan 2006
Location: nyc
Distribution: Slackware 10.2
Posts: 20

Original Poster
Rep: Reputation: 0
#include <iostream> fixed the first half of error message

( #include <iostream.h> && #include "iostream.h" ) dont work

they still give me the hello.cpp:6: error: 'string' undeclared (first use this function)
...

any ideas?
 
Old 01-10-2006, 09:16 PM   #8
cornbeef
LQ Newbie
 
Registered: Jan 2006
Location: nyc
Distribution: Slackware 10.2
Posts: 20

Original Poster
Rep: Reputation: 0
i mean
#include <string.h> "string.h"
 
Old 01-10-2006, 11:14 PM   #9
Cirdan
Member
 
Registered: Nov 2005
Distribution: Arch Linux
Posts: 84

Rep: Reputation: 15
Post your code exacly how it is now, please.
 
Old 01-10-2006, 11:19 PM   #10
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
Quote:
Originally Posted by Cirdan
add .h to <string>

iostream is the only one you don't need .h in it.
No, under the C++ standard, C++ standard headers do not have ".h" in them. I think you are confusing two different header files. There is the header that provides the C++ string class, which you would include with <string>. Then there is also the original C "string.h" header, which you would include in C++ with <cstring>. (You include any other C headers in the same way, e.g. <cstdlib> instead of "stdlib.h").
 
Old 01-11-2006, 12:31 AM   #11
cornbeef
LQ Newbie
 
Registered: Jan 2006
Location: nyc
Distribution: Slackware 10.2
Posts: 20

Original Poster
Rep: Reputation: 0
i finally did a little more research,
and alas

the missing link!!

#include <iostream>
#include "string.h"

using namespace std;
 
Old 01-11-2006, 03:15 AM   #12
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
little shortcut hint for you.
your prog = hello.cpp
g++ hello.cpp produces a.out.

try
make hello and it will compile and link a
nice executable called hello
(if you like that sort of thing of course)

it uses the implicit rule of the make utility.
man make

Last edited by bigearsbilly; 01-11-2006 at 03:17 AM.
 
Old 01-11-2006, 11:26 PM   #13
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by spooon
No, under the C++ standard, C++ standard headers do not have ".h" in them. I think you are confusing two different header files. There is the header that provides the C++ string class, which you would include with <string>. Then there is also the original C "string.h" header, which you would include in C++ with <cstring>. (You include any other C headers in the same way, e.g. <cstdlib> instead of "stdlib.h").
thats a good description of it.. in the new version of the C++ headers you do not use the .h extension. if you use .h you are getting a completely separate file that is there for backwards compatibility.. also cornbeef, the solution you found comes from the fact that you are using the new header files.. like you saw in your first example you do not need the namespace declaration, but you were also using a deprecated version of iostream.. the same goes for the files that spooon listed.. the need to declare
using namespace std;
is because in the new versions of the headers they were put in the std namespace, which was not done previously..

if your tutorial is using the old headers they may also be using other deprecated info.. just be a little wary and question things that dont make sense.. good luck..

oh yeh also.. i dont know if you fixed it or not but your variable _name_ was undeclared in the second example... maybe you already have it solved.. if not..

Code:
#include <iostream>                                 // not iostream.h
#include <string>                                   // using "string.h" looks for file in current directory

using namespace std;                                // <---- you already knew that!..

int main()
{
    string name;                                    // <--------- to use a variable you must declare it
    cout << "\n\n enter your name: ";
    cin >> name;
    cout << "\n your name is: " << name << endl;    // <-- instead of "\n"
    return 0;                                       // not return(0)
}
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Configuring Boot Process on Mandrake 10 to include command "ifup eith0" Bennyjamin Linux - Hardware 3 12-27-2004 04:22 PM
#include<string> not working dtjohnst Linux - Software 2 03-26-2004 08:00 PM
Clarifiacation on header file "/usr/include/a.out.h" swathisat Linux - Software 0 09-05-2003 12:12 AM
sendmail: "permission denied" when using include:/file sohmc Linux - Software 1 05-22-2003 06:40 AM
What is correct code for "Include File Function" on .asp pages gregoryfrancis Linux - General 1 02-11-2003 10:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration