为什么字符串未在范围内声明

Jim*_*imm 21 c++ boost-thread

我有以下代码:

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

static boost::thread_specific_ptr<string> _tssThreadNameSptr;
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

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

tssNaming.h:7:错误:未在此范围内声明'string'

但我在我的包括字符串#include.

Rap*_*ptz 44

你必须使用,std::string因为它在std命名空间中.


小智 8

stringstd命名空间中.您有以下选择:

  • using namespace std;在include之后写入并启用所有std名称:然后您只能string在您的程序上编写.
  • using std::string在include之后写入以启用std::string:然后您只能string在您的程序上编写.
  • std::string而不是string


小智 6

我发现包括:

using namespace std;
Run Code Online (Sandbox Code Playgroud)

对于您的 C++ 代码来说,可以节省大量调试时间,尤其是在像您这样需要 std:: string 的情况下,而且它还有助于保持代码整洁。

考虑到这一点,您的代码应该是:

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

static boost::thread_specific_ptr<string> _tssThreadNameSptr;
Run Code Online (Sandbox Code Playgroud)