40

I have the following code:

#include <string>
#include <boost/thread/tss.hpp>

static boost::thread_specific_ptr<string> _tssThreadNameSptr;

I get the following error

g++ -c -I$BOOST_PATH tssNaming.h

tssNaming.h:7: error: 'string' was not declared in this scope

But I am including string in my #include.

Community
  • 1
  • 1
Jimm
  • 8,165
  • 16
  • 69
  • 118
  • 1
    possible duplicate of [C++ error: ‘string’ has not been declared](http://stackoverflow.com/questions/2890860/c-error-string-has-not-been-declared) – juanchopanza Sep 01 '12 at 19:01

3 Answers3

71

You have to use std::string since it's in the std namespace.

Rapptz
  • 20,807
  • 5
  • 72
  • 86
13

string is in the std namespace. You have the following options:

  • Write using namespace std; after the include and enable all the std names: then you can write only string on your program.
  • Write using std::string after the include to enable std::string: then you can write only string on your program.
  • Use std::string instead of string
  • 1
    You should use 'using namespace std' or 'using std::string' sparingly and in a bounded scope (for example, inside a function). Never use it in a header, since it would pollute the global namespace with symbols users of your header may not want. – alexc Aug 23 '16 at 15:07
6

I find that including:

using namespace std;

To your C++ code saves a lot of time in debugging especially in situations like yours where std:: string is required and also it will help in keeping your code clean.

With this in mind, your code should be:

#include <string>
using namespace std;
#include <boost/thread/tss.hpp>

static boost::thread_specific_ptr<string> _tssThreadNameSptr;
Nuelsian
  • 501
  • 7
  • 18