简体   繁体   中英

Can you declare a pointer as extern in 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.

Yes, you can declare a pointer using extern. Your error is most likely you forgot to qualify using std:: :

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

extern std::ostream *debug;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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