I have a base class Vehicle, a child class Scooter and a class which holds an aggregation relation to Vehicle, called Management.
This is the code of management.h:
class Scooter;
class Management
{
public:
.. //(some methods use the Scooter type)
private:
..
};
This is an example of a method in management.cpp:
bool Management::compare_scooter(std::shared_ptr<Scooter> scooter1, std::shared_ptr<Scooter> scooter2){
return (scooter1->getBatteryLevel() > scooter2->getBatteryLevel());
}
In management.cpp "scooter.h" is included.
Scooter inherits from Vehicle like this:
class Scooter : public Vehicle
Now, my question is: if I try including "scooter.h" inside management.h (leaving the include in management.cpp too) I get this error, why do I get this error:
management.cpp:47:13: error: cannot initialize object parameter of
type ‘const Vehicle’ with an expression of type
‘std::__shared_ptr_access<Scooter, __gnu_cxx::_S_atomic, false,
false>::element_type’ (aka ‘Scooter’)
My second question is, why you wouldn’t be able to just include scooter.h to use Scooter in Managment and instead of using a forward declaration.
I appologise if this is a bit vague, please tell me if this is the case I will try to provide more information.
Edit:
This is the code in the vehicle.h file:
class Vehicle
{
public:
..
protected:
..
};
This is the code in the vehicle.cpp file:
Vehicle::Vehicle(GPSPos where, const Management* comp) :
batteryLevel{100.0f}, currentPosition{where}, startRent{nullptr}, company{comp}
{
}
.. some methods
Source: Windows Questions C++