I ran across some C++ code recently that typedef
ed 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
template<typename S, typename T> class A {
public:
typedef struct {
S x;
T y;
} Datum;
};
#include "A.h"
template<typename Q> class B : public A<Q, Q> {
public:
void output(const Datum& dat);
};
#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
typedef struct {
S x;
T y;
}
is ill-formed. typedef
needs a type and an "alias" to name that type. What you probably need is
template<typename S, typename T>
class A {
public:
struct Datum { S x; T y; };
};
without the typedef, which is not necessary at all here. Then, you need to qualify the Datum
name properly, as typename A<Q,Q>::Datum
:
#include "A.h"
template<typename Q>
class B : public A<Q, Q> {
public:
void output(const typename A<Q,Q>::Datum& dat);
};