why is string not declared in scope

老子叫甜甜 提交于 2019-12-18 12:49:31

问题


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.


回答1:


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




回答2:


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



回答3:


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;


来源:https://stackoverflow.com/questions/12230156/why-is-string-not-declared-in-scope

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!