我需要能够从wager.cpp访问在mainwindow.h中声明的QList<QRadioButton *> colorList
。
我将在这里展示我目前的尝试。
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
错误:LNK2005:“class QList colorList”(?colorList @@ 3V?$ QList @ PAVQRadioButton @@@@ A)已在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在
中定义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 ;
}
答案 0 :(得分:2)
像标题一样在标题中声明它:但声明为extern
:
extern QList<QRadioButton *> colorList; //declared
除了在标题中“声明”它之外,还要在CPP文件中“定义”它:
QList<QRadioButton *> colorList; //defined
在全局范围内定义它,而不是在MainWindow构造函数中定义:
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 ;
}
或者,您可以将其声明并定义为类MainWindow的静态成员。
答案 1 :(得分:0)
您必须使用extern关键字来访问C ++中的全局变量。语法如下:
extern type varName;
请在声明变量的文件中尝试此操作:
extern QList<QRadioButton *> colorList;