Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. I would like some help with inheritance syntax
Forum Updated to NodeBB v4.3 + New Features

I would like some help with inheritance syntax

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 488 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • MDCatoM Offline
    MDCatoM Offline
    MDCato
    wrote on last edited by
    #1

    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!!!

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      It should work. Can you paste the sample code ?

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • KillerSmathK Offline
        KillerSmathK Offline
        KillerSmath
        wrote on last edited by KillerSmath
        #3

        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>
        {
          ...
        };
        

        @Computer Science Student - Brazil
        Web Developer and Researcher
        “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

        MDCatoM 1 Reply Last reply
        8
        • KillerSmathK KillerSmath

          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>
          {
            ...
          };
          
          MDCatoM Offline
          MDCatoM Offline
          MDCato
          wrote on last edited by
          #4

          @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 :)

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved