Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt: Global String Calling Issue



  • Ok i am new to QT and been playing around and watching tutorials but i having issues reverencing a string i want to make global,

    here is my .H

    #ifndef ELFENLIEDTOPFAN5RES_H
    #define ELFENLIEDTOPFAN5RES_H
    #include <QString>
    
    
    class ELFENLIEDTOPFAN5RES
    {
    public:
    ELFENLIEDTOPFAN5RES();
    static QString BO3_Path;
    };
    
    
    
    
    #endif // ELFENLIEDTOPFAN5RES_H
    

    and here is the cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QMessageBox>
    #include <QStatusBar>
    #include "elfenliedtopfan5Res.h"
    
    
    ELFENLIEDTOPFAN5RES::ELFENLIEDTOPFAN5RES()
    {
        ELFENLIEDTOPFAN5RES::BO3_Path = (QString(getenv("TA_GAME_PATH")).replace('\\', '/'));
    
    }
    
    
    MainWindow::MainWindow(QWidget *parent)
    
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
    }
    
    
    MainWindow::~MainWindow()
    {
    
        delete ui;
        //const QString elfenliedtopfan5Res::BO3_PATH = (QString(getenv("TA_GAME_PATH")).replace('\\', '/'));
    }
    
    
    
    void MainWindow::on_pushButton_clicked()
    {
        QString elfen2 = ELFENLIEDTOPFAN5RES::BO3_Path;
       QMessageBox::information(this,"title",elfen2);
    }
    

    this is the error it throws,
    mainwindow.cpp:-1: error: undefined reference to `ELFENLIEDTOPFAN5RES::BO3_Path'

    but unsure of why its doing this i have tried multiple things and looked on loads of global string setups but still no avail
    sorry if its a noob question
    thank you for reading
    elfenliedtopfan5


  • Lifetime Qt Champion

    Basic c++ - a static variable needs to be properly initialized. You only declared that there is a static variable but did not define it.


  • Lifetime Qt Champion

    Basic c++ - a static variable needs to be properly initialized. You only declared that there is a static variable but did not define it.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Move that line out of the constructor. Initializing a static class variable is not done there.

    By the way, Qt provides functions to get environment variables.



  • @Christian-Ehrlicher Thank you sorry i am really new to QT and c++ is not my native language c# is so get little confused but than you for reply :)


Log in to reply