string undeclared identifier?

Started by
1 comment, last by MitchellH 19 years, 5 months ago
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
Advertisement
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.
Thomas - www.moelhave.dk
Thank you very much thooms. That did work although I couldve sworn i tried that before and it was saying that std was not a defined namespace. Oh well, it works now :)

This topic is closed to new replies.

Advertisement