Results 1 to 8 of 8

Thread: simple problem, not sure what I did:

  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
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    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
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    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.

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

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

    mainwindow.h:

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QApplication>
    5. #include <QListWidget>
    6. #include <QMainWindow>
    7. #include <QDialog>
    8. #include <QGroupBox>
    9. #include <QHBoxLayout>
    10. #include <QVBoxLayout>
    11. #include <QPushButton>
    12. #include <QMenuBar>
    13. #include <QMenu>
    14. #include <QLabel>
    15. #include <QString>
    16. #include <string>
    17. #include <vector>
    18. #include <ostream>
    19. #include <fstream>
    20. #include <sstream>
    21. #include <iostream>
    22. #include <cmath>
    23.  
    24. using std::vector;
    25. using std::iostream;
    26. using std::ostream;
    27. using std::fstream;
    28.  
    29. class QAction;
    30. class QGroupBox;
    31. class QLabel;
    32. class QMenuBar;
    33. class QMenu;
    34. class MainWindow : public QMainWindow
    35. {
    36. Q_OBJECT
    37.  
    38. public:
    39. MainWindow();
    40. void analyze_deck();
    41. QListWidgetItem *items_a[10];
    42. public slots:
    43.  
    44. void read_deck(const QString &fileName);
    45. void refresh_deck();
    46. void save_analysis(const std::string &fileName);
    47. void current_list_itemM(QListWidgetItem *item);
    48. void current_list_itemB(QListWidgetItem *item);
    49. void current_list_itemG(QListWidgetItem *item);
    50. void button_pushed(int x);
    51.  
    52. signals:
    53.  
    54.  
    55. private:
    56. void createH_bx_main(); // for main layout
    57. void createH_bx_lst(); //for horizontal button box of main list
    58. void createH_bx_bd(); //"" bad list
    59. void createH_bx_gd(); //"" good list
    60. void createV_bx_lst(); //vertical for main list
    61. void createV_bx_bd(); //""bad list
    62. void createV_bx_gd(); //"" good
    63. void createV_bx_rslt(); //for results
    64. void create_menu(); // for buttons
    65. void create_actions();
    66.  
    67. //const int output_number = 4;
    68. //const int btns_main_menu = 11;
    69.  
    70.  
    71. QHBoxLayout *h_mn_p; //main
    72. QHBoxLayout *h_lst_p; //list
    73. QHBoxLayout *h_bd_p; //bad
    74. QHBoxLayout *h_gd_p; //good
    75.  
    76. QVBoxLayout *v_lst_p;//list
    77. QVBoxLayout *v_bd_p;//bad
    78. QVBoxLayout *v_gd_p;//good
    79. QVBoxLayout *v_rslt_p;//results (vert)
    80.  
    81. QMenu *file;
    82. QMenu *analyze;
    83.  
    84. QLabel *results[10]; //evens are descriptive, odds are numbers
    85.  
    86. QPushButton *btns_a[6];
    87.  
    88. QAction *exitAction;
    89. QAction *analyzeAction;
    90. QAction *refreshAction;
    91. QAction *readAction;
    92. QAction *saveAction;
    93.  
    94. QListWidget *rslt_lst;
    95. QListWidget *bd_lst;
    96. QListWidget *gd_lst;
    97.  
    98.  
    99. };
    100.  
    101. QString current_file;
    102. double nfomf(int n, int m, int k); //n factorial over m factorial for k terms.
    103.  
    104. int list_sizes[3]; // amount in main/bad/good
    105.  
    106. QListWidget *mn_lst;
    107. int monster_count = 0;// monster/spell/trap
    108. int spell_count = 0;
    109. int trap_count = 0;
    110. int whereami[3]; //main/bad/good
    111.  
    112. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7. MainWindow calc;
    8. calc.show();
    9.  
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp:
    Qt Code:
    1. #include "mainwindow.h"
    2. using namespace std;
    3.  
    4. void save_analysis(const std::string &fileName)
    5. {
    6. }
    7.  
    8. double nfomf(int n, int m, int k)
    9. {
    10. double answer = 1;
    11. while((n-k+1) <= m)
    12. {
    13. k--;
    14. m--;
    15. }
    16.  
    17. for(int j = 0; j < k; j++)
    18. {
    19. answer = answer*(n-j)/(m-j);
    20. }
    21. return answer;
    22. }
    23.  
    24. void analyze_deck()
    25. {
    26. double total_cards = monster_count + spell_count + trap_count;
    27. //no monsters:
    28. double no_m = nfomf(total_cards - monster_count, total_cards,6);
    29. //no spells
    30. double no_s = nfomf(total_cards - spell_count, total_cards,6);
    31. //no traps
    32. double no_t = nfomf(total_cards - trap_count, total_cards,6);
    33. //all bad
    34. double allb = nfomf(total_cards - list_sizes[1], total_cards, 6);
    35. //at least 1 good
    36. double al1g = 1 - nfomf(total_cards - list_sizes[2], total_cards, 6);
    37.  
    38. QString no_m_s = QString::number(no_m);
    39. QString no_s_s = QString::number(no_s);
    40. QString no_t_s = QString::number(no_t);
    41. QString allb_s = QString::number(allb);
    42. QString al1g_s = QString::number(al1g);
    43.  
    44. items_a[1]->setText(no_m_s);
    45. items_a[3]->setText(no_s_s);
    46. items_a[5]->setText(no_t_s);
    47. items_a[7]->setText(allb_s);
    48. items_a[9]->setText(al1g_s);
    49. }
    50.  
    51. /*
    52. *returns the line number of the
    53. * card's id number.
    54. */
    55. int read_number(const std::string idnum)
    56. {
    57. int iter = 0;
    58. std::ifstream infile(".../numbers.txt");
    59. std::string line;
    60. while(std::getline(infile, line))
    61. {
    62. if(line == idnum)
    63. {
    64. infile.close();
    65. return iter;
    66. iter++;
    67. }
    68. }
    69. infile.close();
    70. return 0;
    71. }
    72. /*
    73. *returns the name of the card with the
    74. *id number.
    75. */
    76. std::string read_name(const std::string idnum)
    77. {
    78. int line_number = read_number(idnum);
    79.  
    80. std::ifstream infile(".../names");
    81. std::string line;
    82.  
    83. for(int i = 0; i < line_number; i++)
    84. {
    85. getline(infile, line);
    86. }
    87. infile.close();
    88. getline(infile, line);
    89. return line;
    90. }
    91.  
    92.  
    93. /*
    94.  *returns the type of the card, and writes overall
    95.  *balance to vector ctd.
    96. */
    97. int read_type(const std::string idnum)
    98. {
    99. int line_number = read_number(idnum);
    100. std::ifstream infile("...types.txt");
    101. std::string line;
    102.  
    103. for(int i = 0; i < line_number; i++)
    104. {
    105. getline(infile, line);
    106. }
    107.  
    108. getline(infile, line);
    109. int type_number = atoi(line.c_str());
    110.  
    111. if (type_number %16 == 1)
    112. {
    113. infile.close();
    114. monster_count++;
    115. return 0;
    116. }
    117. else if(type_number %16 == 2)
    118. {
    119. infile.close();
    120. spell_count++;
    121. return 1;
    122. }
    123. else
    124. {
    125. infile.close();
    126. trap_count++;
    127. return 2;
    128. }
    129. }
    130.  
    131.  
    132. /*reads deck.txt file, adds the cards' names to the main list widget,
    133.  *and finds the type distribution
    134.  */
    135. void read_deck(const QString &fileName)
    136. {
    137. list_sizes[0] = 0;
    138. list_sizes[1] = 0;
    139. list_sizes[2] = 0;
    140. monster_count = 0;
    141. spell_count = 0;
    142. trap_count = 0;
    143. current_file = fileName;
    144. std::string current_card;
    145. QString current_card_q;
    146. std::ifstream infile("filename.txt");
    147. int passed_main = 0;
    148. std::string line;
    149. while(std::getline(infile, line))
    150. {
    151. if(line == "#main")
    152. {
    153. passed_main++;
    154. }
    155. else if(line == "#extra")
    156. {
    157. passed_main++;
    158. }
    159. else if(passed_main == 1)
    160. {
    161. current_card = read_name(line);
    162. current_card_q = QString::fromUtf8(current_card.c_str());
    163. new_item->setText((current_card_q));
    164. mn_lst->QListWidget::addItem(new_item);
    165. list_sizes[0]++;
    166. read_type(current_card);
    167. }
    168. }
    169. }
    170.  
    171.  
    172. void refresh_deck()
    173. {
    174.  
    175. {
    176. read_deck(current_file);
    177. }
    178. }
    179.  
    180. void MainWindow::create_actions()
    181. {
    182. readAction = new QAction(tr("Read new deck"), this);
    183. QObject::connect(readAction, SIGNAL(triggered()), this, SLOT(read_deck(const QString &fileName)));
    184.  
    185. refreshAction = new QAction(tr("Reload current deck"), this);
    186. QObject::connect(refreshAction, SIGNAL(triggered()), this, SLOT(refresh_deck()));
    187.  
    188. saveAction = new QAction(tr("Save results to file"), this);
    189. QObject::connect(saveAction, SIGNAL(triggered()), this, SLOT(save_analysis()));
    190.  
    191. analyzeAction = new QAction(tr("Perform deck analysis"), this);
    192. QObject::connect(analyzeAction, SIGNAL(triggered()), this, SLOT(analyze_deck()));
    193.  
    194. QObject::connect(exitAction,SIGNAL(triggered()), qApp, SLOT(quit()));
    195.  
    196. }
    197.  
    198. void MainWindow::createV_bx_rslt()
    199. {
    200. v_rslt_p = new QVBoxLayout;
    201.  
    202. QListWidget *rslt_lst = new QListWidget(this);
    203. v_rslt_p->QVBoxLayout::addWidget(rslt_lst);
    204.  
    205. for(int i = 0; i < 10; i++)
    206. {
    207. items_a[i] = new QListWidgetItem;
    208. rslt_lst->addItem(items_a[i]);
    209. }
    210.  
    211. items_a[0]->setText(tr("Probability of starting with no Monster cards"));
    212. //items_a[1]->setText((""));
    213. items_a[2]->setText(tr("Probability of starting with no Spell cards"));
    214. //items_a[3]->setText((""));
    215. items_a[4]->setText(("Probability of starting with no trap cards"));
    216. //items_a[5]->setText((""));
    217. items_a[6]->setText(("Probability of starting with a dead hand"));
    218. //items_a[7]->setText((""));
    219. items_a[8]->setText(tr("Probability of at least one key card in opening hand"));
    220. //items_a[9]->setText((""));
    221. }
    222.  
    223. void MainWindow::createH_bx_lst()
    224. {
    225.  
    226. h_lst_p = new QHBoxLayout;
    227.  
    228. btns_a[0] = new QPushButton(tr("To &Bad"));
    229. btns_a[1] = new QPushButton(tr("To &Good"));
    230.  
    231. h_lst_p->QHBoxLayout::addWidget(btns_a[0]);
    232. h_lst_p->QHBoxLayout::addWidget(btns_a[1]);
    233. }
    234.  
    235. void MainWindow::createH_bx_bd()
    236. {
    237. h_bd_p = new QHBoxLayout;
    238.  
    239. btns_a[2] = new QPushButton(tr("To &Main"), this);
    240. btns_a[3] = new QPushButton(tr("To &Good"), this);
    241.  
    242. h_bd_p->addWidget(btns_a[2]);
    243. h_bd_p->addWidget(btns_a[3]);
    244. }
    245.  
    246. void MainWindow::createH_bx_gd()
    247. {
    248. h_gd_p = new QHBoxLayout;
    249.  
    250. btns_a[5] = new QPushButton(tr("To &Deleterious"));
    251. btns_a[4] = new QPushButton(tr("To &Main"));
    252.  
    253. h_gd_p->QHBoxLayout::addWidget(btns_a[4]);
    254. h_gd_p->QHBoxLayout::addWidget(btns_a[5]);
    255. }
    256.  
    257. void MainWindow::createV_bx_lst()
    258. {
    259. v_lst_p = new QVBoxLayout;
    260.  
    261. QListWidget *mn_lst = new QListWidget(this);
    262. v_lst_p->QVBoxLayout::addWidget(mn_lst);
    263.  
    264. createH_bx_lst();
    265. v_lst_p->QVBoxLayout::addLayout(h_lst_p);
    266. }
    267.  
    268. void MainWindow::createV_bx_bd()
    269. {
    270. v_bd_p = new QVBoxLayout;
    271.  
    272. QListWidget *bd_lst = new QListWidget(this);
    273. v_bd_p->QVBoxLayout::addWidget(bd_lst);
    274.  
    275. createH_bx_bd();
    276. v_bd_p->QVBoxLayout::addLayout(h_bd_p);
    277. }
    278.  
    279. void MainWindow::createV_bx_gd()
    280. {
    281. v_gd_p = new QVBoxLayout;
    282.  
    283. QListWidget *gd_lst = new QListWidget(this);
    284. v_gd_p->QVBoxLayout::addWidget(gd_lst);
    285.  
    286. createH_bx_gd();
    287. v_gd_p->QVBoxLayout::addLayout(h_gd_p);
    288. }
    289.  
    290. void MainWindow::create_menu()
    291. {
    292. file = menuBar()->addMenu(tr("&File"));
    293. file->addAction(readAction);
    294. file->addAction(refreshAction);
    295. file->addAction(exitAction);
    296.  
    297. analyze = menuBar()->addMenu(tr("&Analysis"));
    298. analyze->addAction(analyzeAction);
    299. analyze->addAction(saveAction);
    300. }
    301.  
    302. void MainWindow::createH_bx_main()
    303. {
    304. createV_bx_lst();
    305. createV_bx_bd();
    306. createV_bx_gd();
    307. createV_bx_rslt();
    308.  
    309. QHBoxLayout *h_mn_p = new QHBoxLayout;
    310. h_mn_p->QHBoxLayout::addLayout(v_lst_p);
    311. h_mn_p->QHBoxLayout::addLayout(v_bd_p);
    312. h_mn_p->QHBoxLayout::addLayout(v_gd_p);
    313. h_mn_p->QHBoxLayout::addLayout(v_rslt_p);
    314. }
    315.  
    316. MainWindow::MainWindow()
    317. {
    318. createH_bx_main();
    319. create_actions();
    320. create_menu();
    321. setLayout(h_mn_p);
    322.  
    323. setWindowTitle(tr("Yu-gi-oh Deck Analyzer: Beware the Monte Carlo Fallacy"));
    324. }
    To copy to clipboard, switch view to plain text mode 

    That's everything

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    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.

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

  8. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    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.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.