C++“std::string 尚未声明”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17040098/
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
C++ "std::string has not been declared" error
提问by wannabe programmer
I've been looking for an answer on websites but couldn't find any answer that helped me.
我一直在网站上寻找答案,但找不到任何对我有帮助的答案。
I have a code that uses strings when i tried (like suggested) to add these lines:
当我尝试(如建议的那样)添加这些行时,我有一个使用字符串的代码:
using namespace std;
using std::string;
#include <string>
I tried to use each of them seperately and I tried all of them together. The best situation was when all of the string errors disappeared but I had another weird error on the line "using std::string" and the error was: std::string has not been declared. Any ideas? Thanks guys.
我尝试单独使用它们中的每一个,然后将它们全部一起尝试。最好的情况是所有字符串错误都消失了,但我在“使用 std::string”行上遇到了另一个奇怪的错误,错误是:std::string 尚未声明。有任何想法吗?谢谢你们。
采纳答案by Bathsheba
Put #include <string>first.
#include <string>先放。
Avoid usingstatements in headers as you potentially bring in all sorts of stuff into many compilation units. using std::stringis perhaps acceptable in a header but using namespace stdcertainly isn'tas it will cause so much namespace pollution in all compilation units. The std namespace is continuously expanding (look at all the new stuff in C++), so you don't want to have to fix lots of errors when upgrading your compiler.
避免using在头文件中使用语句,因为您可能会将各种内容引入许多编译单元。using std::string在头文件中可能是可以接受的,但using namespace std肯定不是,因为它会在所有编译单元中造成如此多的命名空间污染。std 命名空间不断扩展(查看 C++ 中的所有新内容),因此您不希望在升级编译器时修复大量错误。
回答by Roee Gavirel
The includeshould come before the using
将include要来的前using
#include <string>
using namespace std;
//using std::string; <-- Needless

