C++ 在页眉中放置变量的位置?

C++ 在页眉中放置变量的位置?,c++,header,C++,Header,我需要能够从wager.cpp访问mainwindow.h中声明的QList colorList 我将在这里展示我当前的尝试 主窗口 #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QPushButton> #include <QTextEdit> #include <QCheckBox> #include <Qlist> #inc

我需要能够从wager.cpp访问mainwindow.h中声明的
QList colorList

我将在这里展示我当前的尝试

主窗口

#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
\ifndef主窗口
#定义主窗口
#包括
#包括
#包括
#包括
#包括
#包括
名称空间用户界面{
类主窗口;
}
类主窗口:公共QMainWindow
{
Q_对象
公众:
显式主窗口(QWidget*parent=0);
~main窗口();
专用插槽:
void runButtonClicked();
私人:
Ui::MainWindow*Ui;
QPushButton*运行按钮;
QTextEdit*runText;
};
QList colorList;//我应该把这个放在哪里??
#endif//main窗口
错误:LNK2005:“类QList colorList”(?colorList@@3V$QList@PAVQRadioButton@@@@A) 已在main.obj中定义

下注

#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;

}
}
#包括“wager.h”
#包括“mainwindow.h”
#包括“deck.h”
打赌
{
}
无效生成\u打赌\u列表()
{

对于(int i=0;i),您必须使用Extn关键字访问C++中的全局变量。
extern type varName;
因此,请在声明变量的文件中尝试以下操作:

extern QList<QRadioButton *> colorList;
extern-QList颜色列表;

在标题中声明它,就像您所做的那样:但将其声明为
外部

extern QList<QRadioButton *> colorList; //declared
extern QList colorList;//已声明
除了在标题中“声明”它之外,还可以在CPP文件中“定义”它:

QList<QRadioButton *> colorList; //defined
QList colorList;//已定义
在全局范围内定义,而不是在主窗口构造函数中定义:

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 ;


}
QList颜色列表;
主窗口::主窗口(QWidget*父窗口):
QMainWindow(父级),
用户界面(新用户界面::主窗口)
{
QRadioButton*street1BetBlack=新的QRadioButton(“黑色”);
QRadioButton*street2BetBlack=新的QRadioButton(“黑色”);
QRadioButton*street3BetBlack=新的QRadioButton(“黑色”);
QRadioButton*street4BetBlack=新QRadioButton(“黑色”);
QRadioButton*street5BetBlack=新的QRadioButton(“黑色”);
QRadioButton*street1BetRed=新QRadioButton(“红色”);
QRadioButton*street2BetRed=新QRadioButton(“红色”);
QRadioButton*street3BetRed=新的QRadioButton(“红色”);
QRadioButton*street4BetRed=新的QRadioButton(“红色”);
QRadioButton*street5BetRed=新的QRadioButton(“红色”);

标头中的colorList变量?不要!所以只需在cpp中将其声明为extern?
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 ;


}