Results 1 to 8 of 8

Thread: simple problem, not sure what I did:

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2013
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default simple problem, not sure what I did:

    Hello,

    I declared in my mainwindow.h file:

    QListWidgetItem *items_a[10];

    under private variables.

    I have a member function

    void analyze_deck()
    {
    double total_cards = monster_count + spell_count + trap_count;
    //no monsters:
    double no_m = nfomf(total_cards - monster_count, total_cards,6);
    //no spells
    double no_s = nfomf(total_cards - spell_count, total_cards,6);
    //no traps
    double no_t = nfomf(total_cards - trap_count, total_cards,6);
    //at least 1 good
    double al1g = 1 - nfomf(total_cards - list_sizes[2], total_cards, 6);
    //all bad
    double al1b = 1 - nfomf(total_cards - list_sizes[1], total_cards, 6);

    *** items_a[1]->setText(QString("%1").arg(no_m));
    items_a[3]->setText(QString("%1").arg(no_s));
    items_a[5]->setText(QString("%1").arg(no_t));
    items_a[7]->setText(QString("%1").arg(al1b));
    items_a[9]->setText(QString("%1").arg(al1g));
    }

    implemented in mainwindow.cpp


    the compiler is returning "items_a was not declared in this scope" at the line marked ***. (if i comment out that line, the error occurs at the next line down)

    I initialize the items_a[0...9] and add them to the appropriate listwidget further down in the file:

    void MainWindow::createV_bx_rslt() //creates a vbox for outputing the data in
    {
    v_rslt_p = new QVBoxLayout;

    QListWidget *rslt_lst = new QListWidget(this);
    v_rslt_p->QVBoxLayout::addWidget(rslt_lst);

    for(int i = 0; i < 10; i++)
    {
    items_a[i] = new QListWidgetItem;
    rslt_lst->addItem(items_a[i]);
    }

    items_a[0]->setText(tr("Probability of starting with no Monster cards"));

    items_a[2]->setText(tr("Probability of starting with no Spell cards"));

    items_a[4]->setText(("Probability of starting with no trap cards"));

    items_a[6]->setText(("Probability of starting with a dead hand"));

    items_a[8]->setText(tr("Probability of at least one key card in opening hand"));

    }

    Thank you in advance for your advice.

    -ABZB

    edit: I attached the .h and .cpp files in question
    Attached Files Attached Files
    Last edited by ABZB; 13th May 2013 at 14:44.

  2. #2
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Qt products
    Qt4 Qt5
    Platforms
    Windows
    Thanks
    92
    Thanked 16 Times in 16 Posts

    Default Re: simple problem, not sure what I did:

    I think you have created array of pointers, which actually isn't necessary.
    Instead do this:
    Qt Code:
    1. In .h file
    2. QList <QListWidgetItem *>items_a;
    3.  
    4. In .cpp file -> c'tor
    5. for(int i = 0; i < 10; i++)
    6. {
    7. items_a.append(new QListWidgetItem);
    8. }
    9.  
    10. Access it using:
    11. items_a.at(i)->setText(tr("Probability of starting with no Monster cards"));
    12.  
    13. Destroy it this way in d'tor:
    14. while (!items_a.isEmpty())
    15. {
    16. delete items_a.takeFirst();
    17. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by rawfool; 13th May 2013 at 14:54.

  3. #3
    Join Date
    May 2013
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: simple problem, not sure what I did:

    Thank you for that advice, but that didn't solve the issue. Based on the color-coding by my ide, it seems that when I first initialize the list widget in createV_box_result, items_a is recognized as a variable from the .h file. however, in the void analyze_deck(), items_a is not color-coded at all. I think that for some reason the existence of items_a is not being carried over to analyze_deck(), but I am at a loss as to why.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: simple problem, not sure what I did:

    Based on the color-coding by my ide, it seems that when I first initialize the list widget in createV_box_result, items_a is recognized as a variable from the .h file. however, in the void analyze_deck(), items_a is not color-coded at all. I think that for some reason the existence of items_a is not being carried over to analyze_deck(), but I am at a loss as to why.
    The color coding as you said is provided by IDE, as a user friendly feature, it has nothing to do with the compiler, and compiler error messages.

    Compiler may not recongnize a symbol even if it were colored by the IDE. Compiler and IDE are two different applications, don't compare the output of one with other, they will never match.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    May 2013
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: simple problem, not sure what I did:

    ... I didn't write the MainWindow:: before the function... That solved it... Thanks!

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: simple problem, not sure what I did:

    Do these modification (Basic C++ syntax issue), and also implement the other functions in the code.
    Qt Code:
    1. //void analyze_deck()
    2. void MainWindow::analyze_deck()
    3. {
    4. ...
    5. }
    6.  
    7. //void save_analysis(const std::string &fileName)
    8. void MainWindow::save_analysis(const std::string &fileName)
    9. {
    10. ...
    11. }
    12.  
    13. //void read_deck(const QString &fileName)
    14. void MainWindow::read_deck(const QString &fileName)
    15. {
    16. ...
    17. }
    18.  
    19. //void refresh_deck()
    20. void MainWindow::refresh_deck()
    21. {
    22. ...
    23. }
    To copy to clipboard, switch view to plain text mode 

    Move all following to mainwindow.cpp
    Qt Code:
    1. QString current_file;
    2. double nfomf(int n, int m, int k); //n factorial over m factorial for k terms.
    3.  
    4. int list_sizes[3]; // amount in main/bad/good
    5.  
    6. QListWidget *mn_lst;
    7. int monster_count = 0;// monster/spell/trap
    8. int spell_count = 0;
    9. int trap_count = 0;
    10. int whereami[3]; //main/bad/good
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  7. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: simple problem, not sure what I did:

    Your code is missing include directives which could be the problem, show us what all you included in mainwindow.h, and mainwindow.cpp, or better post the exact code, don't tailor it.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Simple problem with layout
    By damon_1990 in forum Newbie
    Replies: 2
    Last Post: 17th October 2011, 23:02
  2. problem in simple example for installEventFilter
    By navid in forum Qt Programming
    Replies: 2
    Last Post: 10th December 2009, 19:43
  3. Simple QScrollArea problem
    By jmsbc in forum Qt Programming
    Replies: 4
    Last Post: 2nd December 2008, 18:45
  4. Simple problem
    By s410i in forum Qt Programming
    Replies: 3
    Last Post: 22nd June 2008, 11:12
  5. Simple QtPainter problem
    By rishid in forum Qt Programming
    Replies: 3
    Last Post: 5th February 2008, 21:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.