Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

static_cast from 'QgsVectorLayer *' to 'QgsMapLayer *', which are not related by inheritance, is not allowed

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;

...
}

>Solution :

The definition of the class is missing at the point where the static_cast is used. Just because the definition of the class exists in some header file doesn’t mean that the compiler automatically knows it everywhere the class is referenced. Whichever header file defines this class was not #included, so the only thing that the compiler knows is that the class has been declared.

Line 46 contains only the QgsVectorLayer class definition:

class QgsVectorLayer;

The translation unit #included this header file, directly or indirectly.

The definition of QgsVectorLayer:

class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public
QgsExpressionContextGenerator, … {…}

And this header file was not #included, directly or indirectly.

You’ll need to figure out how to correctly #include the required header files.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading