Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有以下无法编译的遗留 C++ 代码:
#include <stdio.h> #include <iostream> extern ostream *debug;
GCC(g++)抱怨:“'*'令牌之前的预期初始化程序”
环顾四周,将这些声明为外部引用似乎更常见,如下所示:
extern ostream& debug;
为什么指针无效,但引用在这种情况下?
解决方案:
如下所述,真正的问题是缺少 std:: 命名空间说明符。显然,这在较旧的 C++ 代码中很常见。
是的,您可以使用 extern 声明一个指针。您的错误很可能是您忘记使用std:::
std::
// note the header is cstdio in C++. stdio.h is deprecated #include <cstdio> #include <iostream> extern std::ostream *debug;