Why does the child class not see the parent's typedef struct in C++?
-
26-06-2021 - |
Pregunta
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
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?
Solución
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);
};
Otros consejos
You need to say typename A<Q,Q>::Datum
inside B
. Since the base is a template, its names are dependent names and not visible during the first phase, and you need typename
to specify that the name names a type (and not a value or a template).
You should also lose the typedef
. C++ works differently from C, and you should say:
struct Datum { S x; T y };