×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    1

    Operator Overloading

    Hi all,

    I'm new to QT and doing a sample app from Introduction to Design Patterns in C++ with Qt, 2nd Edition (10.1.2 Exercise: CardGame GUI).

    I have a class Card and a classs Hand where I have overloaded the << operator, but I get the message error: invalid operands of types 'Hand*' and 'Card*' to binary 'operator<<' when I compile.

    This are my classes:

    card.h

    Code:
    #ifndef CARD_H
    #define CARD_H
    
    #include <QObject>
    #include <QLabel>
    
    class Card : public QObject
    {
        Q_OBJECT
    public:
        explicit Card(QString name, QObject *parent = 0);
    
        QString name() const {return m_name;}
        QLabel* label(QWidget *parent = 0);
        int value() const {return m_value;}
        bool isAce() const {return m_isAce;}
    
    private:
        QString m_name;
        int m_value;
        bool m_isAce;
    };
    
    #endif // CARD_H
    card.cpp

    Code:
    #include "card.h"
    
    Card::Card(QString name, QObject *parent) :
        QObject(parent)
    {
        QChar value;
    
    
        m_name = name;
        m_isAce = name.startsWith('a', Qt::CaseInsensitive);
        value = name[0].toLower();
        if (value.isDigit())
        {
            m_value = value.digitValue();
        } else if (value == 'a')
        {
            m_value = 1;
        } else
        {
            m_value = 10;
        }
    }
    
    QLabel* Card::label(QWidget *parent) {
    
        QString fileName = QString(":/images/%1.png").arg(m_name);
        QLabel* label = new QLabel(m_name, parent);
        label->setPixmap(QPixmap(fileName));
        return label;
    }
    hand.h

    Code:
    #ifndef HAND_H
    #define HAND_H
    
    #include <QObject>
    
    class Card;
    
    class Hand : public QObject
    {
        Q_OBJECT
    public:
        explicit Hand(QString name, QObject *parent = 0);
        Hand& operator<< (Card* card);
        ~Hand();
    
    signals:
        void handChanged();
        
    public slots:
        
    private:
        QString m_name;
        QList<Card*> m_cards;
    };
    
    #endif // HAND_H
    hand.cpp
    Code:
    #include "hand.h"
    #include "card.h"
    
    Hand::Hand(QString name, QObject *parent) :
        QObject(parent)
    {
        m_name = name;
    }
    
    
    Hand& Hand::operator<< (Card* card) {
        m_cards << card;
        return *this;
    }
    
    Hand::~Hand() {
        qDeleteAll(m_cards);
        m_cards.clear();
    }
    and this is the code I use to test the overloaded operator:

    Code:
        Hand* hand = new Hand("hand", parent);
        Card* card1 = new Card("a1", parent);
    
        hand << card1;
    Any help would be appreciate,

    Alex B.

  2. #2
    Nokia Developer Expert
    Join Date
    Jun 2012
    Posts
    55

    Re: Operator Overloading

    Hey,

    Try using:

    Code:
    *hand << card;
    You need to dereference the Hand pointer to call the actual overloaded operator.


    Br,

    Ville

Similar Threads

  1. operator overloading question
    By erdiay in forum Symbian C++
    Replies: 1
    Last Post: 2011-02-11, 12:04
  2. illegal name overloading
    By vinp in forum Symbian C++
    Replies: 4
    Last Post: 2010-12-10, 00:24
  3. Overloading new opearator
    By Shilpa13 in forum Symbian C++
    Replies: 3
    Last Post: 2009-05-16, 09:18
  4. overloading delete operator
    By mihirg in forum Symbian C++
    Replies: 3
    Last Post: 2007-10-09, 09:02
  5. Overloading of addCommand()
    By namrata in forum Mobile Java General
    Replies: 8
    Last Post: 2007-02-21, 12:29

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •