你能在 C++ 中将指针声明为 extern 吗?
我有以下一些无法编译的遗留 C++ 代码:
#include <stdio.h>
#include <iostream>
extern ostream *debug;
GCC (g++) 抱怨:“在 '*' 标记之前需要初始化程序”
环顾四周,将它们声明为外部引用似乎更常见,如下所示:
extern ostream& debug;
Why is a point not有效,但是在这种情况下有参考吗?
解决方案:
真正的问题,如下所述,是缺少 std:: 命名空间说明符。 显然,这在较旧的 C++ 代码中很常见。
I have the following bit of legacy C++ code that does not compile:
#include <stdio.h>
#include <iostream>
extern ostream *debug;
GCC (g++) complains: "expected initializer before ‘*’ token"
Looking around it seems more common to declare these as external references, like this:
extern ostream& debug;
Why is a pointer not valid, but a reference is in this situation?
SOLUTION:
The real problem, as mentioned below is that the std:: namespace specifier is missing. Apparently, this was common in older C++ code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您可以使用 extern 声明指针。 您的错误很可能是您忘记使用
std::
进行限定:Yes, you can declare a pointer using extern. Your error is most likely you forgot to qualify using
std::
: