PDA

View Full Version : simple problem, not sure what I did:



ABZB
13th May 2013, 14:52
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

rawfool
13th May 2013, 15:23
I think you have created array of pointers, which actually isn't necessary.
Instead do this:

In .h file
QList <QListWidgetItem *>items_a;

In .cpp file -> c'tor
for(int i = 0; i < 10; i++)
{
items_a.append(new QListWidgetItem);
}

Access it using:
items_a.at(i)->setText(tr("Probability of starting with no Monster cards"));

Destroy it this way in d'tor:
while (!items_a.isEmpty())
{
delete items_a.takeFirst();
}

ABZB
13th May 2013, 15:41
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.

Santosh Reddy
13th May 2013, 15:47
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.

ABZB
13th May 2013, 15:52
mainwindow.h:


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QApplication>
#include <QListWidget>
#include <QMainWindow>
#include <QDialog>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QMenuBar>
#include <QMenu>
#include <QLabel>
#include <QString>
#include <string>
#include <vector>
#include <ostream>
#include <fstream>
#include <sstream>
#include <iostream>
#include <cmath>

using std::vector;
using std::iostream;
using std::ostream;
using std::fstream;

class QAction;
class QGroupBox;
class QLabel;
class QPushButton;
class QHBoxLayout;
class QVBoxLayout;
class QListWidget;
class QMenuBar;
class QMenu;
class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow();
void analyze_deck();
QListWidgetItem *items_a[10];
public slots:

void read_deck(const QString &fileName);
void refresh_deck();
void save_analysis(const std::string &fileName);
void current_list_itemM(QListWidgetItem *item);
void current_list_itemB(QListWidgetItem *item);
void current_list_itemG(QListWidgetItem *item);
void button_pushed(int x);

signals:


private:
void createH_bx_main(); // for main layout
void createH_bx_lst(); //for horizontal button box of main list
void createH_bx_bd(); //"" bad list
void createH_bx_gd(); //"" good list
void createV_bx_lst(); //vertical for main list
void createV_bx_bd(); //""bad list
void createV_bx_gd(); //"" good
void createV_bx_rslt(); //for results
void create_menu(); // for buttons
void create_actions();

//const int output_number = 4;
//const int btns_main_menu = 11;


QHBoxLayout *h_mn_p; //main
QHBoxLayout *h_lst_p; //list
QHBoxLayout *h_bd_p; //bad
QHBoxLayout *h_gd_p; //good

QVBoxLayout *v_lst_p;//list
QVBoxLayout *v_bd_p;//bad
QVBoxLayout *v_gd_p;//good
QVBoxLayout *v_rslt_p;//results (vert)

QMenu *file;
QMenu *analyze;

QLabel *results[10]; //evens are descriptive, odds are numbers

QPushButton *btns_a[6];

QAction *exitAction;
QAction *analyzeAction;
QAction *refreshAction;
QAction *readAction;
QAction *saveAction;

QListWidget *rslt_lst;
QListWidget *bd_lst;
QListWidget *gd_lst;


};

QString current_file;
double nfomf(int n, int m, int k); //n factorial over m factorial for k terms.

int list_sizes[3]; // amount in main/bad/good

QListWidget *mn_lst;
int monster_count = 0;// monster/spell/trap
int spell_count = 0;
int trap_count = 0;
int whereami[3]; //main/bad/good

#endif // MAINWINDOW_H


main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow calc;
calc.show();

return app.exec();
}


mainwindow.cpp:

#include "mainwindow.h"
using namespace std;

void save_analysis(const std::string &fileName)
{
}

double nfomf(int n, int m, int k)
{
double answer = 1;
while((n-k+1) <= m)
{
k--;
m--;
}

for(int j = 0; j < k; j++)
{
answer = answer*(n-j)/(m-j);
}
return answer;
}

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);
//all bad
double allb = nfomf(total_cards - list_sizes[1], total_cards, 6);
//at least 1 good
double al1g = 1 - nfomf(total_cards - list_sizes[2], total_cards, 6);

QString no_m_s = QString::number(no_m);
QString no_s_s = QString::number(no_s);
QString no_t_s = QString::number(no_t);
QString allb_s = QString::number(allb);
QString al1g_s = QString::number(al1g);

items_a[1]->setText(no_m_s);
items_a[3]->setText(no_s_s);
items_a[5]->setText(no_t_s);
items_a[7]->setText(allb_s);
items_a[9]->setText(al1g_s);
}

/*
*returns the line number of the
* card's id number.
*/
int read_number(const std::string idnum)
{
int iter = 0;
std::ifstream infile(".../numbers.txt");
std::string line;
while(std::getline(infile, line))
{
if(line == idnum)
{
infile.close();
return iter;
iter++;
}
}
infile.close();
return 0;
}
/*
*returns the name of the card with the
*id number.
*/
std::string read_name(const std::string idnum)
{
int line_number = read_number(idnum);

std::ifstream infile(".../names");
std::string line;

for(int i = 0; i < line_number; i++)
{
getline(infile, line);
}
infile.close();
getline(infile, line);
return line;
}


/*
*returns the type of the card, and writes overall
*balance to vector ctd.
*/
int read_type(const std::string idnum)
{
int line_number = read_number(idnum);
std::ifstream infile("...types.txt");
std::string line;

for(int i = 0; i < line_number; i++)
{
getline(infile, line);
}

getline(infile, line);
int type_number = atoi(line.c_str());

if (type_number %16 == 1)
{
infile.close();
monster_count++;
return 0;
}
else if(type_number %16 == 2)
{
infile.close();
spell_count++;
return 1;
}
else
{
infile.close();
trap_count++;
return 2;
}
}


/*reads deck.txt file, adds the cards' names to the main list widget,
*and finds the type distribution
*/
void read_deck(const QString &fileName)
{
list_sizes[0] = 0;
list_sizes[1] = 0;
list_sizes[2] = 0;
monster_count = 0;
spell_count = 0;
trap_count = 0;
current_file = fileName;
std::string current_card;
QString current_card_q;
std::ifstream infile("filename.txt");
int passed_main = 0;
std::string line;
while(std::getline(infile, line))
{
if(line == "#main")
{
passed_main++;
}
else if(line == "#extra")
{
passed_main++;
}
else if(passed_main == 1)
{
current_card = read_name(line);
current_card_q = QString::fromUtf8(current_card.c_str());
QListWidgetItem *new_item = new QListWidgetItem;
new_item->setText((current_card_q));
mn_lst->QListWidget::addItem(new_item);
list_sizes[0]++;
read_type(current_card);
}
}
}


void refresh_deck()
{

{
read_deck(current_file);
}
}

void MainWindow::create_actions()
{
readAction = new QAction(tr("Read new deck"), this);
QObject::connect(readAction, SIGNAL(triggered()), this, SLOT(read_deck(const QString &fileName)));

refreshAction = new QAction(tr("Reload current deck"), this);
QObject::connect(refreshAction, SIGNAL(triggered()), this, SLOT(refresh_deck()));

saveAction = new QAction(tr("Save results to file"), this);
QObject::connect(saveAction, SIGNAL(triggered()), this, SLOT(save_analysis()));

analyzeAction = new QAction(tr("Perform deck analysis"), this);
QObject::connect(analyzeAction, SIGNAL(triggered()), this, SLOT(analyze_deck()));

QObject::connect(exitAction,SIGNAL(triggered()), qApp, SLOT(quit()));

}

void MainWindow::createV_bx_rslt()
{
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[1]->setText((""));
items_a[2]->setText(tr("Probability of starting with no Spell cards"));
//items_a[3]->setText((""));
items_a[4]->setText(("Probability of starting with no trap cards"));
//items_a[5]->setText((""));
items_a[6]->setText(("Probability of starting with a dead hand"));
//items_a[7]->setText((""));
items_a[8]->setText(tr("Probability of at least one key card in opening hand"));
//items_a[9]->setText((""));
}

void MainWindow::createH_bx_lst()
{

h_lst_p = new QHBoxLayout;

btns_a[0] = new QPushButton(tr("To &Bad"));
btns_a[1] = new QPushButton(tr("To &Good"));

h_lst_p->QHBoxLayout::addWidget(btns_a[0]);
h_lst_p->QHBoxLayout::addWidget(btns_a[1]);
}

void MainWindow::createH_bx_bd()
{
h_bd_p = new QHBoxLayout;

btns_a[2] = new QPushButton(tr("To &Main"), this);
btns_a[3] = new QPushButton(tr("To &Good"), this);

h_bd_p->addWidget(btns_a[2]);
h_bd_p->addWidget(btns_a[3]);
}

void MainWindow::createH_bx_gd()
{
h_gd_p = new QHBoxLayout;

btns_a[5] = new QPushButton(tr("To &Deleterious"));
btns_a[4] = new QPushButton(tr("To &Main"));

h_gd_p->QHBoxLayout::addWidget(btns_a[4]);
h_gd_p->QHBoxLayout::addWidget(btns_a[5]);
}

void MainWindow::createV_bx_lst()
{
v_lst_p = new QVBoxLayout;

QListWidget *mn_lst = new QListWidget(this);
v_lst_p->QVBoxLayout::addWidget(mn_lst);

createH_bx_lst();
v_lst_p->QVBoxLayout::addLayout(h_lst_p);
}

void MainWindow::createV_bx_bd()
{
v_bd_p = new QVBoxLayout;

QListWidget *bd_lst = new QListWidget(this);
v_bd_p->QVBoxLayout::addWidget(bd_lst);

createH_bx_bd();
v_bd_p->QVBoxLayout::addLayout(h_bd_p);
}

void MainWindow::createV_bx_gd()
{
v_gd_p = new QVBoxLayout;

QListWidget *gd_lst = new QListWidget(this);
v_gd_p->QVBoxLayout::addWidget(gd_lst);

createH_bx_gd();
v_gd_p->QVBoxLayout::addLayout(h_gd_p);
}

void MainWindow::create_menu()
{
file = menuBar()->addMenu(tr("&File"));
file->addAction(readAction);
file->addAction(refreshAction);
file->addAction(exitAction);

analyze = menuBar()->addMenu(tr("&Analysis"));
analyze->addAction(analyzeAction);
analyze->addAction(saveAction);
}

void MainWindow::createH_bx_main()
{
createV_bx_lst();
createV_bx_bd();
createV_bx_gd();
createV_bx_rslt();

QHBoxLayout *h_mn_p = new QHBoxLayout;
h_mn_p->QHBoxLayout::addLayout(v_lst_p);
h_mn_p->QHBoxLayout::addLayout(v_bd_p);
h_mn_p->QHBoxLayout::addLayout(v_gd_p);
h_mn_p->QHBoxLayout::addLayout(v_rslt_p);
}

MainWindow::MainWindow()
{
createH_bx_main();
create_actions();
create_menu();
setLayout(h_mn_p);

setWindowTitle(tr("Yu-gi-oh Deck Analyzer: Beware the Monte Carlo Fallacy"));
}


That's everything

Santosh Reddy
13th May 2013, 15:52
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.

ABZB
13th May 2013, 15:58
... I didn't write the MainWindow:: before the function... That solved it... Thanks!

Santosh Reddy
13th May 2013, 16:02
Do these modification (Basic C++ syntax issue), and also implement the other functions in the code.


//void analyze_deck()
void MainWindow::analyze_deck()
{
...
}

//void save_analysis(const std::string &fileName)
void MainWindow::save_analysis(const std::string &fileName)
{
...
}

//void read_deck(const QString &fileName)
void MainWindow::read_deck(const QString &fileName)
{
...
}

//void refresh_deck()
void MainWindow::refresh_deck()
{
...
}


Move all following to mainwindow.cpp


QString current_file;
double nfomf(int n, int m, int k); //n factorial over m factorial for k terms.

int list_sizes[3]; // amount in main/bad/good

QListWidget *mn_lst;
int monster_count = 0;// monster/spell/trap
int spell_count = 0;
int trap_count = 0;
int whereami[3]; //main/bad/good