Home > Mobile >  QT c multiple definition and first defined here error
QT c multiple definition and first defined here error

Time:04-28

I added a part in the main.cpp file that asks for a password before the window opens. It gets what it needs from the help.h and cnstnt.h files. Then I created a dialog named settings and tried to change the password here. It was working fine in my previous test project, but when I used the same things in this project, I encountered a first defined here error. I checked, I did run qmake and rebuild but nothing changed. How can I fix this problem? I'm new to C and QT.

here is my codes

main.cpp

#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

help.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

settingdialog.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

settingdialog.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!");
       }
    }



}

errors

  1. X:\DataLoggerQT\QtSerialMonitor-master\src\help.h:11: error: multiple definition of hlpr::passwordHash(QByteArray)' debug/mainwindow.o: In function ZSt19__iterator_categoryIPK7QStringENSt15iterator_traitsIT_E17iterator_categoryERKS4_': X:\DataLoggerQT\QtSerialMonitor-master\build-QtSerialMonitor-Desktop_Qt_5_15_2_MinGW_32_bit-Debug/../src/help.h:11: multiple definition of `hlpr::passwordHash(QByteArray)'
  2. X:\DataLoggerQT\QtSerialMonitor-master\src\help.h:11: first defined here
  3. X:\DataLoggerQT\QtSerialMonitor-master\src\help.h:28: error: multiple definition of hlpr::setTxtPassword(QString)' debug/moc_mainwindow.o: In function ZN4hlpr14setTxtPasswordE7QString': X:\DataLoggerQT\QtSerialMonitor-master\build-QtSerialMonitor-Desktop_Qt_5_15_2_MinGW_32_bit-Debug/debug/../../src/help.h:28: multiple definition of `hlpr::setTxtPassword(QString)'

CodePudding user response:

You have included helper.h in two header files - settingdialog.h and main.cpp, mainwindow.h is included in main.cpp and, I suppose, includes settingdialog.h itself. Or, maybe, it's included through any other included header. So you've got multiple definition error, one per include. To avoid such kind of errors, functions in helper.h file should be declared with extern keyword and their implementation should be moved to helper.cpp file. It would prevent you from any potential problems in future. And you should always declare functions in this way. And at last, you do not need to include helper.h in settingdialog.h as you use it's functions in cpp. Move include to settingdialog.cpp. Keep it in mind to include files only there, where they are really used, it would minimize compilation time.

  •  Tags:  
  • c qt
  • Related