简体   繁体   English

您可以在C ++中将指针声明为extern吗?

[英]Can you declare a pointer as extern in C++?

I have the following bit of legacy C++ code that does not compile: 我有以下旧的C ++代码无法编译:

#include <stdio.h>
#include <iostream>

extern ostream *debug;

GCC (g++) complains: "expected initializer before '*' token" GCC(g ++)抱怨:“'*'标记之前有预期的初始化程序”

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. 真正的问题(如下所述)是缺少std ::名称空间说明符。 Apparently, this was common in older C++ code. 显然,这在较旧的C ++代码中很常见。

Yes, you can declare a pointer using extern. 是的,您可以使用extern声明一个指针。 Your error is most likely you forgot to qualify using std:: : 您的错误很可能是您忘记使用std::

// note the header is cstdio in C++. stdio.h is deprecated
#include <cstdio>
#include <iostream>

extern std::ostream *debug;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM