ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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***
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").
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)
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.