Hi,
C:\Users\thund_000\Desktop\Class Project\CMedical.cpp||In constructor 'CMedical::CMedical(const string&)':|
C:\Users\thund_000\Desktop\Class Project\CMedical.cpp|6|error: no matching function for call to 'CResource::CResource()'| |
The
Name
variable is in the
CResource
class, so a derived class has to call the
CResource
constructor, just as the compiler error says. The same is true for any derived class, they have to call a constructor of it's parent - in order to initialise the parent class member variables.
...... I'm a visual learner, I learn by looking at something that's completed and then dissecting it, ...... |
As I was saying earlier, the classes you need to create are very similar to ones that I had. So all you need to do is the the same as I what I did. Obviously the classes are going to have their own variables, but the general idea of how they work is the same.
Look at the
CResource
class, The parameters to it's constructor are the Name, which is initialised in the initialiser list.
Look at the
CVehicle
class. The parameters to it's constructor are the Name, and the two variables that belong to the
CVehicle
class. In the initialiser list, there is a call to the
CResource
constructor, with Name as the argument. Then the two variables that belong to the class are initialised.
Now look at the
CFourWheelDrive
class. It's parameters are the Name, the two values that belong to the member variables in the
CVehicle
class, and values that belong to the member variables of the
CFourWheelDrive
class. In the initialiser list, the constructor for the
CVehicle
class is called with the arguments it needs. Then the variables that belong to the class are initialised.
So can you see the pattern here? A constructor for a derived class takes arguments (parameters) in order to initialise it's parent class, which in turn initialises it parent class, and so on until the very top base class is initialised. Note that the base class is
created first, then successive derived classes, which is the reverse order of how they are called.
With the polymorphism, there is a pure virtual function called
PrintDetails
in the
CResource
class. A virtual function means that the function
may be redefined by a derived class. A pure virtual function means that the function
must be redefined by derived classes, so this forces specialised behaviour for the derived classes. An ordinary virtual function allows the same behaviour to apply to many derived classes, but the function is only defined once.
Now in
CPlayer
we have the std::vector Gear,
which it is very important to note that it holds pointers to CResource
This is important because
a pointer to a Derived class is a valid pointer to a Base class.
So the
AcquireResource
function can push a pointer to any of the derived
CResource
classes (FourWheelDrive, MediPack, Assault rifle say) into the Gear vector.
Now for the best part: In the
CPlayer::ShowResources
function when the the
PrintDetails
function is called for each of the pointers in the Gear vector, the correct function is called. The beauty of this is that each class has a function with the same name, so we can extend our inheritance tree without having to change things everywhere.
I hope this is enough information for you to fix up your existing code, and write some new code - remember it is similar to what you have already.
Hope all goes well. :+)