I ran across some C++ code recently that typedefed a struct in a parent class. However, it seemed to be unavailable in the child class, even tho it was not marked private (it was protected). I’ve created a minimal working example (below) below that demonstrates this failure. I have turned everything public and it still fails. The error that is given is (using g++):
B.h:8: error: expected ',' or '...' before '&' token
B.h.8: error: ISO C++ forbids declartion of 'Datum' with no type
A.h (compiles)
template<typename S, typename T> class A {
public:
typedef struct {
S x;
T y;
} Datum;
};
B.h (does not compile)
#include "A.h"
template<typename Q> class B : public A<Q, Q> {
public:
void output(const Datum& dat);
};
B.h (compiles)
#include "A.h"
template<typename Q> class B : public A<Q, Q> {
public:
typedef struct {
Q x;
Q y;
} Datum;
void output(const Datum& dat);
};
Why does the first version of B.h not compile? Is the second one a safe alternative? Is there a better (more concise or idiomatic) way to handle this?
This
is ill-formed.
typedefneeds a type and an “alias” to name that type. What you probably need iswithout the typedef, which is not necessary at all here. Then, you need to qualify the
Datumname properly, astypename A<Q,Q>::Datum: