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. class member update error
Forum Updated to NodeBB v4.3 + New Features

class member update error

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 1.1k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    msdberkani
    wrote on last edited by
    #1

    Hello,
    I'am new in Qt, can you please help me solve a problem:
    1: I have class cardpics
    class CardPics : public QObject {
    public:
    explicit CardPics(QObject* parent=0);
    ~CardPics() ;
    QImage get(QString card) const;
    Card* pick();
    int getIndice();// renvoie un indice aléaatoire du paquet
    void reset_m_indice();
    int numberCardLeft()const;
    static const QString values;
    static const QString suits;
    int m_increment;
    void increment();
    protected:
    static QString fileName(QString card);
    private:
    static const int TAILLE_MAX=52;
    int m_nbrCartePickee;
    QMap<QString, QImage> m_images;
    QList<Card*> m_cards;
    int m_indice[TAILLE_MAX];
    QStringList m_stringCards;
    };
    2. the pick methode of cardpick:bolded text
    Card* CardPics::pick(){
    m_increment++;
    m_nbrCartePickee=m_nbrCartePickee+1;
    Card* crdTemp= new Card(m_stringCards.at(getIndice()));
    return crdTemp;
    }
    3. the getIndice from CardPickbolded text
    int CardPics::getIndice(){
    bool isOK(true);
    int indice0(99);
    while (isOK){
    indice0 = qrand() % TAILLE_MAX;
    qDebug()<< m_indice[indice0];
    if (m_indice[indice0]!= (99)) isOK=false;
    }
    int indice1= m_indice[indice0];
    m_indice[indice0]=99;
    return indice1;
    }
    4**.the numbercardleft from cardpick**
    int CardPics::numberCardLeft() const {
    /qDebug()<<"-++-++-++-++-++------\n";
    qDebug()<< m_nbrCartePickee;
    qDebug()<<"!+!+!+!+!+!+!+!+!+!+\n";
    qDebug()<< m_nbrCartePickee;
    //qDebug()<< m_increment;
    qDebug()<<" \n !+!+!+!+!+!+!+!+!+!+\n";
    return TAILLE_MAX-m_nbrCartePickee;
    }
    5. the class cardpick is memeber of class blackjack: (m_cardDeck)
    BlackJack::BlackJack(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::BlackJack),m_handDealer(), m_handPlayer()
    {
    .....
    m_cardPick= new CardPick;
    ....
    m_handDealer<<m_cardDeck->pick();
    m_handDealer<<m_cardDeck->pick();

    m_handPlayer<<m_cardDeck->pick();
    m_handPlayer<<m_cardDeck->pick();
    A: test one:
    int nTemp(0);
    nTemp=m_cardPick-->numberCardLeft();
    B: test two:
    m_handPlayer<<m_cardPick-->pick()
    nTemp=m_cardPick-->numberCardLeft();
    }
    ///// So I have somme problem with the update of the parameters m_increment and m_nbrCartePickee
    in A, test one: (it's ok)
    m_increment >> 4
    m_nbrCartePickee>> 4
    in B: test two: (problem)
    m_increment >> 226810788
    m_nbrCartePickee>> 0

    Do you have some idea why these value dont behave like I think I write the code: in test B I want to have
    m_increment >> 5
    m_nbrCartePickee>> 5

    Thank you very much.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      The problem you are talking about has nothing to do with Qt itself, it's rather general C++.

      Did you correctly initialise all your member variables in the constructor of your class?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • M Offline
        M Offline
        msdberkani
        wrote on last edited by
        #3

        Hi, and thank you for your answer,

        Yes I initialized all the members correctly, I try to find out the problem but I have no idea where the problem is.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Can you post a complete minimum sample of your code that shows the behaviour ? Don't forget to use coding tags. That will make things easier.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • M Offline
            M Offline
            msdberkani
            wrote on last edited by
            #5

            Thank you for you response,
            I finaly fin the error:

            the problem was:

            class A{
            public:
            A::A(){
            Class B* m_property= new class B
            }
            .//function
            A::fct1(){
            m_m_property->doSomething();
            }
            private:
            Class B* m_property;
            }

            when I replace the redefinition Class B*m_property= new class B by m_property= new class B all thing are OK. I don't know why but it works.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Because you were shadowing your member variable. Basically it's having a local variable with the same name as a class member variable.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              3

              • Login

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