# String does not name a type

This topic is 4180 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have this
#include <fstream.h>
#include <string.h>
#include <iostream>

. . . code

string fileName;


And i get this error: string does not name a type. I cant figure it out. Thanks.

##### Share on other sites
Did you remember the namespace?
If not then try std::string.

##### Share on other sites
That worked, thanks.

##### Share on other sites
Also, unless you're specifically also wanting functions for old C-style strings, you shouldn't be using <string.h>. Instead, just use <string>. The only reason your current code is working is because either <fstream.h> (which should also have the ".h" removed) or <iostream> (or both) are including <string> for you.

<string.h> is for C-style string functions.

<fstream.h> is a deprecated header that got supersceded by <fstream>

##### Share on other sites
Quote:
 Original post by Unconquerable#include #include #include . . . codestring fileName;

0) Please immediately destroy* whatever reference told you to write that. It is many years out of date and is distributing misinformation that really irritates those of us who try to help out beginners - it's simply disgusting that we have to clean up these kinds of messes so many years later.

1) string.h does not define the string data type. It instead provides the old C string-manipulation functions. However, you should only use that in C programs. If you are writing a C++ program and for some reason you need those functions (which you should not, because you should be using real strings), include cstring instead. To get the real string type, include string. All of the C++ standard library headers (the ones that you use angle brackets to include, and which are not from some third party library, e.g. windows.h) have no .h extension.

2) Similarly, fstream.h does not exist any more. It should just be 'fstream', just like iostream is just iostream.

3) The string data type lives in the std::namespace, like everything else defined by the standard C++ library. You must either qualify the name explicitly as 'std::string', "use" the name via a separate statement "using std::string;" (then using 'string' for the type name), or "use" the entire std:: namespace via a separate statement "using namespace std;".

"using" things does not, and cannot, imply any kind of performance penalty, but it does have consequences in terms of how you write your code: you can't "un-use" something within a scope, and you will have problems if you want to create your own type with the same name. (By explicitly specifying std::string vs. your own string, you can keep the names separate; using-declarations exist for the times when you don't need that flexibility and want to save typing. Of course, you shouldn't make your own string type anyway, but some things in the std:: namespace have rather more likely-to-collide names, like 'distance'.) For this reason, you should not put "using" statements at top level in a header file, because then everyone who includes the file is stripped of the option of *not* "using".

* Assuming it's your property, of course. If it's a website, for example, please instead contact the author and explain the situation politely. Please feel free to cite this thread, as well as any references you might find around here - I could have sworn this stuff made its way into one FAQ or another by now...