我一直在网站上寻找答案,但找不到任何对我有帮助的答案。
当我尝试(如建议的)添加这些行时,我有一个使用字符串的代码:
using namespace std;
using std::string;
#include <string>
我尝试单独使用它们中的每一个,然后将它们一起尝试。最好的情况是所有字符串错误都消失了,但我在“使用 std::string”这一行出现了另一个奇怪的错误,错误是:std::string 尚未声明。有任何想法吗?多谢你们。
先放#include <string>
。
避免using
在标头中使用语句,因为您可能会将各种东西带入许多编译单元。using std::string
在头文件中可能是可以接受的,但using namespace std
肯定不是,因为它会在所有编译单元中造成如此多的命名空间污染。std 命名空间在不断扩展(查看 C++ 中的所有新内容),因此您不希望在升级编译器时修复大量错误。
include
应该在using
#include <string>
using namespace std;
//using std::string; <-- Needless