我在 main.cpp 檔案中添加了一個在視窗打開之前要求輸入密碼的部分。它從 help.h 和 cnstnt.h 檔案中獲取所需的內容。然后我創建了一個名為設定的對話框并嘗試在此處更改密碼。它在我之前的測驗專案中運行良好,但是當我在這個專案中使用相同的東西時,我遇到了 first defined here 錯誤。我檢查了一下,我確實運行了 qmake 并重建,但沒有任何改變。我該如何解決這個問題?我是 C 和 QT 的新手。
這是我的代碼
主檔案
#include "mainwindow.h"
#include <QApplication>
#include <QMessageBox>
#include "cnstnt.h"
#include "help.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setStyle("fusion");
a.setQuitOnLastWindowClosed(false);
MainWindow w;
QString login = QInputDialog::getText(NULL, "Login","username",QLineEdit::Normal);
if (login == cnstnt::username)
{
QString getPassword = QInputDialog::getText(NULL, "Login","password",QLineEdit::Password);
QString hashpassword = hlpr::hashPassword(getPassword.toUtf8());
if(hashpassword == hlpr::getTxtPassword()){
w.show();
}else{
QMessageBox::warning(nullptr, "error!", "wrong password!");
}
}
else
{
QMessageBox::warning(nullptr, "error!", "wrong username!");
}
return a.exec();
}
cnstnt.h
#ifndef CNSTNT_H
#define CNSTNT_H
#include <QtWidgets>
namespace cnstnt {
QString TEXT_DIR = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) "/test/password.txt";
QString username = "admin";
}
#endif // CNSTNT_H
幫助.h
#ifndef HELP_H
#define HELP_H
#include <QCryptographicHash>
#include "cnstnt.h"
namespace hlpr {
// login actions
QString hashPassword(QByteArray str){
QByteArray step1 = QCryptographicHash::hash((str),QCryptographicHash::Md5).toHex();
QString lastHash = QString(QCryptographicHash::hash((step1),QCryptographicHash::Sha512).toHex());
return lastHash;
}
QString getTxtPassword(){
QString currentPassword;
QFile file(cnstnt::TEXT_DIR);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)){
QTextStream in(&file);
currentPassword = in.readLine();
file.close();
}
return currentPassword;
}
bool setTxtPassword(QString newPass){
QFile file(cnstnt::TEXT_DIR);
if(file.open(QIODevice::WriteOnly | QIODevice::Truncate)){
QTextStream stream(&file);
QByteArray newPassType = newPass.toUtf8();
stream << hashPassword(newPassType);
file.close();
return true;
}
return false;
}
}
#endif // HELP_H
設定對話框.h
#ifndef SETTINGDIALOG_H
#define SETTINGDIALOG_H
#include <QDialog>
#include <QMessageBox>
#include "help.h"
namespace Ui {
class settingDialog;
}
class settingDialog : public QDialog
{
Q_OBJECT
public:
explicit settingDialog(QWidget *parent = nullptr);
~settingDialog();
private slots:
void on_pushButton_8_clicked();
private:
Ui::settingDialog *ui;
};
#endif // SETTINGDIALOG_H
設定對話框.cpp
#include "settingdialog.h"
#include "ui_settingdialog.h"
settingDialog::settingDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::settingDialog)
{
ui->setupUi(this);
}
settingDialog::~settingDialog()
{
delete ui;
}
void settingDialog::on_pushButton_8_clicked()
{
QString TEXT_DIR = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) "/test/password.txt";
QString savedPassword = hlpr::getTxtPassword();
QString curPassword = ui->oldPassBox->text();
QString newPass = ui->newPassBox->text();
QString confirmPass = ui->confirmPass->text();
QByteArray curHash = curPassword.toUtf8();
if(newPass != confirmPass) {
QMessageBox::information(this, "Bilgi", "Yeni ?ifreniz ile tekrar? e?le?miyor!");
}else if(newPass == curPassword){
QMessageBox::information(this, "Bilgi", "Mevcut ?ifreniz ile yeni ?ifreniz ile ayn? olamaz!");
}else if(savedPassword != hlpr::hashPassword(curHash)) {
QMessageBox::information(this, "Bilgi", "Mevcut ?ifreniz hatal?!");
}else{
bool status = hlpr::setTxtPassword(newPass);
if(status){
ui->oldPassBox->clear();
ui->newPassBox->clear();
ui->confirmPass->clear();
QMessageBox::information(this, "Ba?ar?l?", "?ifreniz ba?ar?yla güncellendi.");
}else{
QMessageBox::warning(this, "Hata!", "?ifreniz güncellenirken hata olu?tu!");
}
}
}
錯誤
- X:\DataLoggerQT\QtSerialMonitor-master\src\help.h:11: 錯誤:
hlpr::passwordHash(QByteArray)' debug/mainwindow.o: In functionZSt19__iterator_categoryIPK7QStringENSt15iterator_traitsIT_E17iterator_categoryERKS4_'的多個定義:X:\DataLoggerQT\QtSerialMonitor-master\build-QtSerialMonitor-Desktop_Qt_5_15_2/MinGW_/help.3. :11: `hlpr::passwordHash(QByteArray)' 的多重定義 - X:\DataLoggerQT\QtSerialMonitor-master\src\help.h:11:首先定義在這里
- X:\DataLoggerQT\QtSerialMonitor-master\src\help.h:28: 錯誤:
hlpr::setTxtPassword(QString)' debug/moc_mainwindow.o: In functionZN4hlpr14setTxtPasswordE7QString'的多個定義:X:\DataLoggerQT\QtSerialMonitor-master\build-QtSerialMonitor-Desktop_Qt_5_15_2_MinGW_32_bit-Debug/debug/././ src/help.h:28: `hlpr::setTxtPassword(QString)' 的多重定義
uj5u.com熱心網友回復:
您已將 helper.h 包含在兩個頭檔案中 - settingdialog.h 和 main.cpp,mainwindow.h 包含在 main.cpp 中,并且我想包含 settingdialog.h 本身。或者,它可能包含在任何其他包含的標頭中。所以你有多個定義錯誤,每個包含一個。為避免此類錯誤,helper.h 檔案中的函式應使用extern關鍵字宣告,其實作應移至 helper.cpp 檔案。它將防止您將來出現任何潛在的問題。而且您應該始終以這種方式宣告函式。最后,您不需要在 settingdialog.h 中包含 helper.h,因為您在 cpp 中使用它的功能。將 include 移至 settingdialog.cpp。請記住,僅在真正使用它們的地方包含檔案,這將最大限度地減少編譯時間。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/466406.html
