Reputation: 1835
I need to be able to access QList<QRadioButton *> colorList
which is declared in mainwindow.h, from wager.cpp.
I will show my current attempt here.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QTextEdit>
#include <QCheckBox>
#include <Qlist>
#include <QRadioButton>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void runButtonClicked();
private:
Ui::MainWindow *ui;
QPushButton *runButton;
QTextEdit * runText;
};
QList<QRadioButton *> colorList; // where should i put this??
#endif // MAINWINDOW_H
error: LNK2005: "class QList colorList" (?colorList@@3V?$QList@PAVQRadioButton@@@@A) already defined in main.obj
wager.cpp
#include "wager.h"
#include "mainwindow.h"
#include "deck.h"
Wager::Wager()
{
}
void build_bet_lists()
{
for(int i=0;i<5;i++)
{
qDebug()<<colorList[i]->isChecked;
}
}
colorList is defined in
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
QRadioButton * street1BetBlack = new QRadioButton("Black");
QRadioButton * street2BetBlack = new QRadioButton("Black");
QRadioButton * street3BetBlack = new QRadioButton("Black");
QRadioButton * street4BetBlack = new QRadioButton("Black");
QRadioButton * street5BetBlack = new QRadioButton("Black");
QRadioButton * street1BetRed = new QRadioButton("Red");
QRadioButton * street2BetRed = new QRadioButton("Red");
QRadioButton * street3BetRed = new QRadioButton("Red");
QRadioButton * street4BetRed = new QRadioButton("Red");
QRadioButton * street5BetRed = new QRadioButton("Red");
QList<QRadioButton *> colorList;
colorList << street1BetBlack << street1BetRed << street2BetBlack << street2BetRed << street3BetBlack << street3BetRed << street4BetBlack << street4BetRed << street5BetBlack << street5BetRed ;
}
Upvotes: 0
Views: 173
Reputation: 56113
Declare it in the header, as you did: but declare it as extern
:
extern QList<QRadioButton *> colorList; //declared
As well as "declaring" it in the header, also "define" it in the CPP file:
QList<QRadioButton *> colorList; //defined
Define it at global scope, and not within the MainWindow constructor:
QList<QRadioButton *> colorList;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
QRadioButton * street1BetBlack = new QRadioButton("Black");
QRadioButton * street2BetBlack = new QRadioButton("Black");
QRadioButton * street3BetBlack = new QRadioButton("Black");
QRadioButton * street4BetBlack = new QRadioButton("Black");
QRadioButton * street5BetBlack = new QRadioButton("Black");
QRadioButton * street1BetRed = new QRadioButton("Red");
QRadioButton * street2BetRed = new QRadioButton("Red");
QRadioButton * street3BetRed = new QRadioButton("Red");
QRadioButton * street4BetRed = new QRadioButton("Red");
QRadioButton * street5BetRed = new QRadioButton("Red");
colorList << street1BetBlack << street1BetRed << street2BetBlack << street2BetRed << street3BetBlack << street3BetRed << street4BetBlack << street4BetRed << street5BetBlack << street5BetRed ;
}
Alternatively you can declare and define it as a static member of a class like MainWindow.
Upvotes: 2
Reputation: 2259
You have to use the extern keyword to access global variables in C++. The syntax goes like:
extern type varName;
So try this in the file where you declare the variable:
extern QList<QRadioButton *> colorList;
Upvotes: 0