C++ 为什么字符串没有在范围内声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12230156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:03:09  来源:igfitidea点击:

why is string not declared in scope

c++boost-thread

提问by Jimm

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

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

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

But I am including string in my #include.

但我在我的#include.

回答by Rapptz

You have to use std::stringsince it's in the stdnamespace.

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

回答by Jimm

stringis in the stdnamespace. You have the following options:

string位于std命名空间中。您有以下选择:

  • Write using namespace std;after the include and enable all the stdnames: then you can write only stringon your program.
  • Write using std::stringafter the include to enable std::string: then you can write only stringon your program.
  • Use std::stringinstead of string
  • using namespace std;在包含之后写入并启用所有std名称:然后您只能string在您的程序上写入。
  • using std::string在包含后写入以启用std::string:然后您只能string在您的程序上写入。
  • 使用std::string代替string

回答by Nuelsian

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.

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

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;