Using strings in .h file

Why do I get an error when using string in a .h file?

Error: 'string' does not name a type

When I use string with the main function it works fine. But, when I try to use the string class in a .h file I get the error, even though I included the string library. Also, the code works fine if I use a char* type instead of string.

Thanks for any help.

.cpp code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "Signature.h"

using namespace std;

int main()
{
	// signature block
	cout << SIGNATURE << endl;
	
	// request user's name and store in a variable
	string name;
	cout << "What is your name? ";
	getline( cin, name );
	
	// print "Hello" and the user's name
	cout << "Hello, " << name << "!" << endl;
		
	return 0;
}

Signature.h (where the error is coming from):
1
2
3
4
5
6
7
8
9
#include <string>

const string SIGNATURE = 
	"-----------------------------\n" 
	"George Jones\n"
	"george.jones@gmail.com\n"
	"C++ Programming\n"
	"Fall 2013\n"
	"-----------------------------\n"; 
Last edited on
In the .h file, change string to std::string on line 3
From what I can tell you didn't declare using namespace std; which string is under the std namespace.

There are two ways to solve this,

1
2
3
4
5
6
7
8
9
10
 
#include<string>
using namespace std;
const string SIGNATURE = 
	"-----------------------------\n" 
	"George Jones\n"
	"george.jones@gmail.com\n"
	"C++ Programming\n"
	"Fall 2013\n"
	"-----------------------------\n";


Or you can do

1
2
3
4
5
6
7
8
9
#include <string>

const std::string SIGNATURE =   // <- Notice the std:: before string
	"-----------------------------\n" 
	"George Jones\n"
	"george.jones@gmail.com\n"
	"C++ Programming\n"
	"Fall 2013\n"
	"-----------------------------\n"; 

@Austin J
It is generally bad practice to put using namespace std; in a header file. Well, it's bad practice to use it at all, but putting it in a header file forces all of those unwanted consequences upon any file which includes that header.
OK. Thanks for the help, it worked. Couple questions: Is it necessary to include the string library (#include <string>)? The code seems to work fine without it. Is string part of the the std namespace?
Yes, you should put the #include <string> in the header file. Without it, you are dependent upon that being indirectly included either by some other standard header, or in the file where the header is itself included.

Is string part of the the std namespace? Yes, that's why putting std:: in front of it works.
Thanks for the clarification Chevril.
@Chervil
Yes I agree with you sorry I was vague,I disagree with using namespace for any namespace in general. I suppose I should have mentioned it's more appropriate to use std:: I always go with std::vector<T>someVector over
using namespace std; /* later on */ vector<T>someVector

A good example navig8tr of why you should just go with typing the namespace each time is the SFML vector and STL vector. While you probably don't know what either of those are, you'll still understand my point.

std::vector or STL vector is a container, and for whatever reason was named a vector.

SFML vector can hold two values in one, commonly x,y coordinates.

I wouldn't be surprised if there's an API out there that has a "real" vector. A representation of an objects/forces direction and magnitude.

While the creator of SFML made sure not to name SFML vectors exactly as vector it's still good to keep them easy to differentiate on sight. So you use std:: before STL vector and sf:: before SFML vector.

I personally haven't used Boost but from what I can tell it's pretty important to keep your namespaces obvious when using it.
If you're going to use a variable for your signature, you should probably use a const char* in this case.

If you use a std::string you might well end up allocating memory on the heap pointlessly (I don't know if compilers are smart enough to, or allowed to, strip out the std::string construction.)

1
2
3
4
5
6
7
char const * const SIGNATURE =
	"-----------------------------\n" 
	"George Jones\n"
	"george.jones@gmail.com\n"
	"C++ Programming\n"
	"Fall 2013\n"
	"-----------------------------\n"; 


(That's a const pointer to a const string.)

Andy
Topic archived. No new replies allowed.