Class QgsVectorlayer derives from base class QgsMapLayer which derives from base class QObject. I want to cast a QgsVectorlayer object to a base class. This should easily be possbile but I get an error and don't understand why.
The annex to the (probably not unimportant) error message is:
...qgsgeometry.h:46:7: note: 'QgsVectorLayer' is incomplete
Line 46 contains only the QgsVectorLayer class definition:
class QgsVectorLayer;
(see source https://github.com/qgis/QGIS/blob/master/src/core/geometry/qgsgeometry.h#L46)
This is my use case:
I need to pass the argument variable vectorLayer in my adapted function QgsOgrProvider::extent(QgsVectorLayer* vectorLayer) const to another function as type QgsMapLayer* or QObject* which QgsVectorlayer is derived from. My understanding is that this should automatically be type casted or I could cast it using static_cast<type>(variable) but I get an error and don't understand why.
The definition of QgsVectorLayer:
class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionContextGenerator, ...
{...}
(full source: https://github.com/qgis/QGIS/blob/master/src/core/vector/qgsvectorlayer.h)
The definition of QgsMapLayer:
class CORE_EXPORT QgsMapLayer : public QObject
{...}
(full source: https://github.com/qgis/QGIS/blob/master/src/core/qgsmaplayer.h)
My code that throws errors:
QgsOgrProvider::extent(QgsVectorLayer* vectorLayer) const
{
...
QgsMapLayer * mapLayer=static_cast< QgsMapLayer*>(vectorLayer);
// error: static_cast from 'QgsVectorLayer *' to 'QObject *', which are not related by inheritance, is not allowed; qgsgeometry.h:46:7: note: 'QgsVectorLayer' is incomplete
QObject * mapObject=static_cast< QObject*>(vectorLayer);
// error: static_cast from 'QgsVectorLayer *' to 'QgsMapLayer *', which are not related by inheritance, is not allowed; qgsgeometry.h:46:7: note: 'QgsVectorLayer' is incomplete
QObject * mapObject2=new QObject();
// error: assigning to 'QObject *' from incompatible type 'QgsVectorLayer *'
mapObject2=vectorLayer;
...
}