在我的头文件中,我得到了
错误:“字符串”尚未声明
错误,但在文件顶部我有
#include <string>
,那么我怎么会收到此错误?
string
位于 std
命名空间中,您必须使用 std::string
或通过 using 指令或 using 声明将其引入作用域。
使用
std::string var;
或
using namespace std;
string var;
字符串位于 std 命名空间中,因此您必须让编译器知道。
标准库定义的大多数类和类模板以及这些类的实例(如
string
和 cout
)都是在 std::
命名空间中声明的,而不是在全局 命名空间 中声明的。因此,每当您想使用(例如)string
时,该类就是 实际上 std::string
。
每次使用这样的类时,您都可以显式地添加名称空间前缀,但这可能会变得乏味。为了提供帮助,您可以为您经常使用的类添加 using
声明。 因此,在您的情况下,您可以添加如下所示的行,通常在相应的
#include
行之后不久的位置:
#include <string>
#include <iostream>
using std::string;
using std::cout; // Added as illustrative examples of other
using std::endl; // ... elements of the Standard Library
或者,如果您有支持 C++17 标准(或更高版本)的编译器,则可以在同一条语句中放置多个类:
using std::string, std::cout, std::endl; // C++17 or later
但是,请注意使用通用的“包罗万象”using namespace std;
指令(即使这实际上是有效的 C++ 语法)。请参阅:为什么是“using namespace std;”被认为是不好的做法吗?
C++ 标准库)的更一般介绍和概述,请参阅 标签-Wiki 了解 stl 标签。
示例 1.x 提供了
using
的用法(并非严格特定于字符串)。示例 2.x 提供了
using
和
#include <...>
的用法(特定于字符串)。
using
的格式:
library::methodOrItemFromThatLibrary
include
的格式(对于官方C++库):
#include <officialLibraryName>
示例 1.0(将会失败):// Provides "cout" as an available method, but you still
// must call the correct library when you use "cout".
// Importantly, you don't call "iostream::cout", but rather
// you call "std::cout"
// Also provides "endl"
#include <iostream>
// Unnecessary when printing "hard-coded" (known text) strings
// directly to console using "cout".
// #include <string>
// using std::string;
int main(){
// Doesn't work because "cout" and "endl" couldn't be found.
// "cout" and "endl" both exist in the std library (thanks
// to "#include <iostream>"), but the compiler doesn't know
// that it should look in the std library to get the
// methods/items "cout" and "endl".
// Since other libraries may have their own implementations of
// those methods/items, the compiler CANNOT assume that the
// std library is the correct library to use.
cout << "hi!" << endl << endl;
// ^ error: ‘cout’ was not declared in this scope; did you mean ‘std::cout’?
}
示例1.1(将会失败):#include <iostream>
int main(){
// Doesn't work because "endl" couldn't be found.
// error: ‘endl’ was not declared in this scope; did you mean ‘std::endl’?
std::cout << "hi!" << std::endl << endl;
// ^^^^ is the culprit.
// Should be "std::endl"
// Every single time you use the item/method, you must
// say which library it's implemented in.
}
示例1.2(将会成功):#include <iostream>
int main(){
// Compiler knows to use the std library (which is where
// library "iostream" made "cout" and "endl"
// addressable) to get the methods/items "cout" and "endl"
std::cout << "hi!" << std::endl << std::endl;
}
示例1.3(将会成功):#include <iostream>
// Tells compiler to assume that when we call "cout",
// we ALWAYS actually mean "std::cout"
using std::cout;
int main(){
cout << "hi!" << std::endl << std::endl;
}
示例1.4(将会成功):#include <iostream>
using std::cout; // cout ==> std::cout
using std::endl; // endl ==> std::endl
int main(){
cout << "hi!" << endl << endl;
}
示例1.5(将会成功):#include <iostream>
// cout ==> std::cout, endl ==> std::endl, string ==> std::string, ...
using namespace std;
int main(){
cout << "hi!" << endl << endl;
}
示例 2.0(将会失败):#include <string>
int main(){
// Doesn't work because type "string" couldn't be found.
// "string" exists in the std library (thanks to
// "#include <string>"), but you didn't tell
// the compiler to look in the std library.
string str = "Hi!";
// ^ error: ‘string’ was not declared in this scope; did you mean ‘std::string’?
}
示例2.1(将会失败):// Even though "string" normally resides in the std library,
// "string" was never "#include"d via "#include <string>",
// so the "string" implementation doesn't exist,
// even if you access it the correct way when calling it.
using std::string;
// ^ error: ‘string’ has not been declared in ‘std’
int main(){
string str = "Hi!";
// ^ error: ‘string’ was not declared in this scope
}
示例2.2(将会成功):#include <string>
using std::string;
int main(){
string str = "Hi!";
}
示例2.3(将会成功):#include <string>
int main(){
std::string str = "Hi!";
}
示例2.4(将会成功):#include <string>
// cout ==> std::cout, endl ==> std::endl, string ==> std::string, ...
using namespace std;
int main(){
string str = "Hi!";
}
** 假设每个 main() 函数内部并同时在末尾有一个 return 0;
,以满足函数 main() 的
int
返回类型。