Reputation: 67
I have a pair of base-derived classes ( IND
= base of MA
, MA
= derived from IND
; ST
= base of ST1
, ST1
= derived from ST
) and another class (FD
). I'm trying to use them like:
class ST1: public ST{
public:
FD f;
ST1(){};
ST1(FD& a) : f(a) {};
MA abc(f, 10);
};
The errors I'm getting:
E0757 member "ST1::f" is not a type name
E0079 expected a type specifier
C2061 identifier 'f'
All errors are on the MA abc(f, 10);
line.
Please note that MA
doesn't have default constructor, takes FD&, int
arguments and IND
is a pure virtual class.
Upvotes: 0
Views: 1227
Reputation: 66371
MA abc(f, 10);
looks to the compiler like a member function declaration, hence the error message - it expects f
and 10
to be names of types.
For in-line member variable initialisation, you must use curly braces:
MA abc {f, 10};
but this isn't of much use to you as that would use f
before you've initialised it.
(In-line initialisation is performed before any constructor initialisation; the order you write them in the class definition is irrelevant.)
Move its initialisation to the initialiser list instead:
ST1() : abc(f,10) {}
ST1(FD& a) : f(a), abc(f,10) {};
and leave the declaration as
MA abc;
Upvotes: 1