LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   #include "string.h" is not working? (https://www.linuxquestions.org/questions/programming-9/include-string-h-is-not-working-401823/)

cornbeef 01-10-2006 09:53 PM

#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***

dmail 01-10-2006 09:56 PM

try using
Code:

#include <string>
instead

cornbeef 01-10-2006 10:02 PM

just tried this for the headers

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

...


got the same error- anything else to try?

Cirdan 01-10-2006 10:04 PM

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

iostream.h is deprecated(sp?).

cornbeef 01-10-2006 10:06 PM

#include <iostream>
#include <string>

same error =(

Cirdan 01-10-2006 10:09 PM

add .h to <string>

iostream is the only one you don't need .h in it.

cornbeef 01-10-2006 10:15 PM

#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?

cornbeef 01-10-2006 10:16 PM

i mean
#include <string.h> "string.h"

Cirdan 01-11-2006 12:14 AM

Post your code exacly how it is now, please.

spooon 01-11-2006 12:19 AM

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").

cornbeef 01-11-2006 01:31 AM

i finally did a little more research,
and alas

the missing link!!

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

using namespace std;

bigearsbilly 01-11-2006 04:15 AM

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

xhi 01-12-2006 12:26 AM

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)
}



All times are GMT -5. The time now is 11:10 AM.