在QT C++中未定义对全局变量的引用。

问题描述 投票:0回答:3

我试图从mainwindow.cpp向userdetails.cpp传递字符串值。我已经使用了全局变量。当程序运行时,它显示错误信息未定义对 globelusername 的引用". globelusername是指全局变量名。代码中的错误是什么?

mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include "register.h"
    #include "userdetails.h"
    //#include <QtSql>
    //#include <QSqlDatabase>
    //#include <QMessageBox>

    extern QString globelusername;

    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE

    class MainWindow : public QMainWindow
    {
        Q_OBJECT

    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();

mainwindow.cpp

    #include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui ->loginusername->setPlaceholderText("Username");
    ui ->loginpassword->setPlaceholderText("Password");
    QString username = ui ->loginusername ->text();
    QString globelusername = username;
     return ;//
}

MainWindow::~MainWindow()
{
    delete ui;

    }  

void MainWindow::on_pushButton_clicked()
{
        //open new registration in new window
    hide();
    regist = new Register(this);
    regist ->show();

}

void MainWindow::on_pushButton_2_clicked()
{
    //database connection
     .....

     QString username = ui ->loginusername ->text();
     QString password = ui ->loginpassword ->text();

     if(db.open()){

         //query create

         QSqlQuery query(QSqlDatabase::database("MyConnect"));

         query.prepare(QString("SELECT * FROM user_reg_elec WHERE username = :username AND password = :password"));

         query.bindValue(":username", username);
         query.bindValue(":password", password);

         QString globelusername = username; //globlevariable
       }

userdetails.cpp

#include "userdetails.h"
#include "ui_userdetails.h"
#include <QSqlError>
#include "mainwindow.h"

Userdetails::Userdetails(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Userdetails)

{
    ui->setupUi(this);

}

Userdetails::~Userdetails()
{

}

void Userdetails::on_pushButton_4_clicked()
{

    {
            // database connection
            ........

            QString abc = globelusername;
c++ string qt parameters qstring
3个回答
1
投票

这一行

extern QString globelusername;

只是声明了一个全局变量,但没有定义它。

你必须在你的 .cpp 档案 mainwindow.cpp):

#include "mainwindow.h"
#include "ui_mainwindow.h"

QString globelusername;

// ...

1
投票

你在头文件的顶部声明了一个全局变量。

extern QString globelusername;

但是你从来没有定义过它。

你在函数内有局部定义,但这些变量并不是你可能认为的全局变量。 它们只是临时变量,当函数的包围范围返回时就会消失。

QString globelusername = username; //globlevariable

为了解决这个问题,在函数的顶部定义这个 mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

QString globelusername;  // add this line


MainWindow::MainWindow(QWidget *parent)

然后在所有你在函数中定义globelusername的地方把它改为只引用顶部的变量(即删除QString类型delclaration,这样编译器就知道它是一个赋值,而不是一个新的变量)。

globelusername = username;

1
投票

你是在拍你自己的脚。

细看 MainWindows.h 是包括一个 Userdetails.h 和用户详情类中包含一个MainWindows.h,这在软件开发中是被调用的,循环依赖,是非常非常糟糕的!相反,在主窗口和Userdetails中定义一个Qstring作为成员对象变量,之后在UserDetails类中定义一个setter,然后在mainWindows中你可以把它作为参数传递给用户。

相反,在主窗口和Userdetails中定义一个Qstring作为成员对象变量,然后在UserDetails类中定义一个setter,然后在mainWindows中你可以把它作为参数传递。

在UserDetail中

Userdetails::setName(const QString& name)
{
    this->name=name;
}

而在主窗口的

MainWindow::foo()
{
    this->myUserDetails->setName(this->name);

}

而后

this->myUserDetails->show(); //exec() or similar
© www.soinside.com 2019 - 2024. All rights reserved.