Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Trying to create a simple subclass



  • #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.


  • Moderators

    @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



  • @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....


Log in to reply