Can't seem to implement the string header properly

Hi,

I'm using C++ on Red Had Linux 8.0. I'm using g++ to compile my programs. I've properly inserted the <iostream> header, but it seems that the others I try do not work correctly.

For instance, I've been trying to use <string> and <fstream> but both give me errors. When I use #include <string> and then implement the string code in my program, such as string x = "test"; I receive the following error:

block_good.cc: In function `int main()':
block_good.cc:58: `string' undeclared (first use this function)
block_good.cc:58: (Each undeclared identifier is reported only once for each
   function it appears in.)
block_good.cc:58: parse error before `=' token

Any help would be great.

Thanks!
bgriscomAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mlmccCommented:
try
#include <string.h>

mlmcc
0
bgriscomAuthor Commented:
I tried changing it to string.h. No dice.
0
bgriscomAuthor Commented:
I tried changing it to string.h. No dice.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

JZ4096Commented:
Perhaps the standard namespace has not been included. Try inserting

#include <string>
using std::string;

or

#include <string>
using namespace std;

at the beginning of the file.

The former brings std::string into the global namespace, whereas the latter brings all elements of the standard namespace.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mlmccCommented:
Is the file string.h and filestream.h in the correct directory on the machine?

mlmcc
0
bgriscomAuthor Commented:

Using the #include <string> and "using std::string" fixes the problem!

I don't understand why I didn't have to do this in my computer science courses. Do I need to modify a header file to include this automatically?

Thanks for the assistance.
0
mlmccCommented:
Did you happen to use MS VC++?  If so it can be setup to not requie the std:

gnu I believe requires it.  There is a way around it but I forget what it is.

SOmething like
Using <String.h>

mlmcc
0
bgriscomAuthor Commented:
Thanks to everyone for the help. If you have any ideas as to why I never had to include this before or if it was built into my header file, please let me know.

Thanks.

Thanks to mlmcc and JZ4096 for the assistance.
0
JZ4096Commented:
Including <string.h> does not include C++'s new STL string class, but only C's string manipulation functions (strcat, strlen, strcmp, etc).

Including <string> includes the STL string, but it is in namespace std by default.  To bring it into the global namespace, using namespace std; or using std::string; is necessary.

~JZ4096
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.