mainwindow.cpp에서 userdetails.cpp로 문자열 값을 전달하려고합니다. 나는 전역 변수를 사용했습니다. 그리고 프로그램이 실행되면 " Undefined references to 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;
이 라인
extern QString globelusername;
전역 변수를 선언하지만 정의하지는 않습니다.
.cpp 파일 중 하나에 정의해야 합니다 (예 : mainwindow.cpp ).
#include "mainwindow.h"
#include "ui_mainwindow.h"
QString globelusername;
// ...
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다