Question

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.

Was it helpful?

Solution

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top