I would like some help with inheritance syntax

  • I want to create a new class called MyMap, and I would like it to inherit the QMap class so that I can extend it's properties, but I keep getting the error:

    "error: expected class-name before '{' token"

    Is this not possible to do with QMap? if I swap out QMap for something like QObject that error goes away. Any help in this area will be most appreciated. Thank you.

    PS i would love a code snipit :) ty!!!

  • Qt Champions 2017

    It should work. Can you paste the sample code ?

  • Hi @MDCato and welcome to Qt Forum.

    QMap is not a class but a class template, so, you need to specify which type of QMap that you are trying to inherit.

    You can decide between 2 approaches:
    1) Create a new class template
    2) Inherit a specific class from QMap class template.

    Create new class template - CustomMap<Key,Value>

    #include <QMap>
    template <class Key, class Value>
    class CustomMap : public QMap<Key, Value>

    Create CustomMap that inherits from QMap<QString, int> (QString as Key, Int as Value)

    #include <QMap>
    #include <QString>
    class CustomMap : public QMap<QString, int>

  • @KillerSmath, that was exactly what I was looking for. I never would have figured that one out by myself in a million years,

    Thank you for your help :)

Log in to reply