我有以下未编译的遗留 C++ 代码:
#include <stdio.h>
#include <iostream>
extern ostream *debug;
GCC (g++) 提示:“在‘*’标记之前需要初始值设定项”
环顾四周,将它们声明为外部引用似乎更常见,如下所示:
extern ostream& debug;
为什么在这种情况下指针无效,但引用有效?
解决方案:
真正的问题,如下所述,是缺少 std:: namespace 说明符。显然,这在旧的 C++ 代码中很常见。
最佳答案
是的,您可以使用 extern 声明一个指针。您的错误很可能是您忘记了使用 std::
进行限定:
// note the header is cstdio in C++. stdio.h is deprecated
#include <cstdio>
#include <iostream>
extern std::ostream *debug;
关于c++ - 你能在 C++ 中将指针声明为 extern 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/366134/