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
why is string not declared in scope
提问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::string
since it's in the std
namespace.
您必须使用,std::string
因为它在std
命名空间中。
回答by Jimm
string
is in the std
namespace. You have the following options:
string
位于std
命名空间中。您有以下选择:
- Write
using namespace std;
after the include and enable all thestd
names: then you can write onlystring
on your program. - Write
using std::string
after the include to enablestd::string
: then you can write onlystring
on your program. - Use
std::string
instead ofstring
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;