【问题标题】:Undefined references to global variable in QT C++QT C++中对全局变量的未定义引用
【发布时间】:2020-10-05 21:22:43
【问题描述】:

我正在尝试将字符串值从 mainwindow.cpp 传递给 userdetails.cpp。我一直使用全局变量。当程序运行时,它会显示错误消息“Undefined references to globelusername”。 globelusername 表示全局变量名。代码有什么错误?

主窗口.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();

主窗口.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;

【问题讨论】:

  • 你首先不应该为此使用全局变量。
  • 此外,您正在使用 qt,为什么不避开整个全局,并在您需要的事件上尝试一个信号/插槽?但事实上,你仍然应该定义你的全局,在你声明它之后:)
  • 没有理由这样做...您正在创建具有循环依赖关系的意大利面条代码...
  • 知道了。谢谢

标签: c++ string qt parameters qstring


【解决方案1】:

你是在打自己的脚,

仔细查看MainWindows.h 包含Userdetails.h 并且用户详细信息包含在软件开发中调用的 MainWindows.h。循环依赖,非常非常糟糕!

相反,在主窗口和 Userdetails 中将 Qstring 定义为成员对象/变量,然后在 UserDetails 类中定义一个 setter,然后在 mainWindows 中您可以将其作为参数传递:

在 uderDetail

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

在主窗口中

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

}

以后再做

this->myUserDetails->show(); //exec() or similar

【讨论】:

    【解决方案2】:

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

    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 类型声明,以便编译器知道它是一个赋值而不是一个新变量)

    globelusername = username;
    

    【讨论】:

    • 我还有一个问题。当我尝试这个“qDebug(globelusername.toLatin1())”时,在 userdetails.cpp 文件中得到空值。
    【解决方案3】:

    这一行

    extern QString globelusername;
    

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

    您必须在您的 .cpp 文件之一中定义它(例如在 mainwindow.cpp 中):

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    QString globelusername;
    
    // ...
    

    【讨论】:

      猜你喜欢
      • 2015-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-08
      • 2018-09-17
      相关资源
      最近更新 更多