Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Trying to create a simple subclass
Forum Updated to NodeBB v4.3 + New Features

Trying to create a simple subclass

Scheduled Pinned Locked Moved Unsolved C++ Gurus
oop subclass c+
3 Posts 3 Posters 647 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.
  • F Offline
    F Offline
    ffej2ffej
    wrote on last edited by Christian Ehrlicher
    #1
    #ifndef CARD_H
    #define CARD_H
    #include <QGraphicsPixmapItem>
    
    class Card : public QGraphicsPixmapItem
    {
        int suit,rank;//I'm working with the belief that putting them here (before the public: declaration) makes them private.
    public:
        Card();
        Card(int,int);//constructor so the card can be initialized.  In fact, I don't think I'll ever use the other one: card()
        int getSuit();
        int getRank();
    };
    
    #endif // CARD_H
    

    The above code is the very beginning of my million billionth attempt to make a useful program in c++. As usual, the syntax is right, the supplied argument is right, everything is right, but somehow the compiler found a way to generate an error that clearly makes no sense and says that what I've just used as a superclass is NOT a class, even though we all know it is. I'm trying to make a card game and make the cards a subclass of QGraphicsPixmapItem.
    Now that I look at the text I've copied above, I see that the error was not copied nor pasted. On the line that defines this as a class and a public subclass of QGraphicsPixmapItem, it says, "expected class name" as if QGraphicsPixmapItem is not.
    I've made sure I've included the class, I've tried all sorts of changes in the syntax and everything else I can think of and all it ever does is make different errors.
    I get so frustrated because I've understood the basic (pun intended) concepts of programming since I was about 12 years old, but every project gets stopped dead because of some error(s) like this.

    J.HilkJ 1 Reply Last reply
    0
    • F ffej2ffej
      #ifndef CARD_H
      #define CARD_H
      #include <QGraphicsPixmapItem>
      
      class Card : public QGraphicsPixmapItem
      {
          int suit,rank;//I'm working with the belief that putting them here (before the public: declaration) makes them private.
      public:
          Card();
          Card(int,int);//constructor so the card can be initialized.  In fact, I don't think I'll ever use the other one: card()
          int getSuit();
          int getRank();
      };
      
      #endif // CARD_H
      

      The above code is the very beginning of my million billionth attempt to make a useful program in c++. As usual, the syntax is right, the supplied argument is right, everything is right, but somehow the compiler found a way to generate an error that clearly makes no sense and says that what I've just used as a superclass is NOT a class, even though we all know it is. I'm trying to make a card game and make the cards a subclass of QGraphicsPixmapItem.
      Now that I look at the text I've copied above, I see that the error was not copied nor pasted. On the line that defines this as a class and a public subclass of QGraphicsPixmapItem, it says, "expected class name" as if QGraphicsPixmapItem is not.
      I've made sure I've included the class, I've tried all sorts of changes in the syntax and everything else I can think of and all it ever does is make different errors.
      I get so frustrated because I've understood the basic (pun intended) concepts of programming since I was about 12 years old, but every project gets stopped dead because of some error(s) like this.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @ffej2ffej
      compiles totally fine for me

      #ifndef CARD_H
      #define CARD_H
      
      #include <QGraphicsPixmapItem>
      
      class Card : public QGraphicsPixmapItem
      {
          int m_suit, m_rank;//I'm working with the belief that putting them here (before the public: declaration) makes them private.
          //!It does make them private indeed
      public:
          //Default constructor
          explicit Card(QGraphicsItem* parent) : QGraphicsPixmapItem(parent) {}
          explicit Card(int suit,int rank)
              : QGraphicsPixmapItem(nullptr), m_suit{suit}, m_rank{rank}{}//constructor so the card can be initialized.  In fact, I don't think I'll ever use the other one: card()
      
      
          [[nodiscard]] constexpr int getSuit() const noexcept{ return m_suit;}
          [[nodiscard]] constexpr int getRank() const noexcept{ return m_rank;}
      };
      
      #endif // CARD_H
      
      

      try deleting your shadow build project, maybe old build artifacts cause problems here


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      JonBJ 1 Reply Last reply
      1
      • J.HilkJ J.Hilk

        @ffej2ffej
        compiles totally fine for me

        #ifndef CARD_H
        #define CARD_H
        
        #include <QGraphicsPixmapItem>
        
        class Card : public QGraphicsPixmapItem
        {
            int m_suit, m_rank;//I'm working with the belief that putting them here (before the public: declaration) makes them private.
            //!It does make them private indeed
        public:
            //Default constructor
            explicit Card(QGraphicsItem* parent) : QGraphicsPixmapItem(parent) {}
            explicit Card(int suit,int rank)
                : QGraphicsPixmapItem(nullptr), m_suit{suit}, m_rank{rank}{}//constructor so the card can be initialized.  In fact, I don't think I'll ever use the other one: card()
        
        
            [[nodiscard]] constexpr int getSuit() const noexcept{ return m_suit;}
            [[nodiscard]] constexpr int getRank() const noexcept{ return m_rank;}
        };
        
        #endif // CARD_H
        
        

        try deleting your shadow build project, maybe old build artifacts cause problems here

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @J-Hilk said in Trying to create a simple subclass:

        try deleting your shadow build project, maybe old build artifacts cause problems here

        @ffej2ffej
        As @J-Hilk says. It should not be like this, but it's a nasty lesson to learn with Qt building in practice: whenever you get an "inexplicable" compile/link message and you are "sure" your code is correct, start by deleting all the contents of the build output directory and rebuild from scratch, before puzzling further. This applies particularly when you have made a change in a header file to do with classes inheriting from QObject/changing whether they have Q_OBJECT macro, and similar....

        1 Reply Last reply
        0

        • Login

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