class member update error



  • 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;
    };

    1. 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;
      }
    2. 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;
      }
    3. 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.


  • Lifetime Qt Champion

    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?



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


  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.