I have a base class and the derived one:
class Neuron
{
protected:
double input;
double output;
};
class InputNeuron : public Neuron
{
public:
InputNeuron();
};
The default constructor of the derived class is defined as follows
InputNeuron::InputNeuron() : input(0.0), output(0.0) {}
The problem is: The initializations of input and output are faulty.
My goal here is benefit from inheritance to avert redeclaring input and output in derived classes. However, in the current state, the use of those members raise a message: input is not a nonstatic data member or base class of class InputNeuron
, message I can't seem to derive information from.
A class constructor can only initialize the (direct) data members of the class; it does not initialize base class data members. Instead, you need to use a base class constructor for that. You will need to define an appropriate base constructor first. If you only want derived classes to use it, make it protected
:
class Neuron
{
protected:
Neuron(double i, double o) : input(i), output(o) {}
private:
double input;
double output;
};
class InputNeuron : public Neuron
{
public:
InputNeuron() : Neuron(0, 0) {}
// ^^^^^^^^^^^^
};
(You could also retain your protected base data members and then assign to them in the bodies of the derived constructors, but that's not good style, since it's best to initialize variables immediately with their intended value.)