Sign in to follow this  
MitchellH

string undeclared identifier?

Recommended Posts

Im trying to use the C++ string object but when I compile, its yelling at me that its undeclared. I include string.h and looked through string.h and its not there
#include <stdio.h>
#include <iostream.h>
#include <string.h>

int main(int argc, char* argv[])
{
	string name = "";
	cin >> name;
	cout << name;

	return 0;
}

Its a useless program since I just want the compile to work for now. EDIT: Also, Im using VC++6

Share this post


Link to post
Share on other sites
Try this instead:


#include <iostream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
string name = "";
cin >> name;
cout << name;

return 0;
}







string.h and iostream.h are deprecated headers, standard C++ headers should be included without the ".h" extension. The symbols in the standard headers are placed in the "std" namespace, which is why I have added a "using namespace std;" to your original program.

I have removed "stdio.h" from your code since you never used it, if you, however, want to use something found in there (or in some of the other ANSI-C headers) you just strip the h extension and prepend a "c", so to include "stdio.h" you would do:

#include <cstdio>


For more info you could check http://www.cplusplus.com/doc/ansi/hfiles.html.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this