[Solved] undefined reference to static QString



  • @
    ...//heather
    QString getNewBigliettoToBeWritten();
    void setNewBigliettoToBeWritten(const QString &value);

    private:
    static QString currentSezione, newBigliettoToBeWritten;
    @

    @
    //.cpp file
    ProgramSettings::ProgramSettings(QObject *parent) :
    QObject(parent)
    {
    //vedi metodi in heather
    newBigliettoToBeWritten = QString("");
    currentSezione = QString("");
    }

    QString ProgramSettings::getNewBigliettoToBeWritten()
    {
    return ProgramSettings::newBigliettoToBeWritten;
    }

    void ProgramSettings::setNewBigliettoToBeWritten(const QString &value)
    {
    if(value != newBigliettoToBeWritten && value != QString("") && !value.isNull())
    newBigliettoToBeWritten = value;
    }
    @

    errors:
    @
    ./debug\widget1.o:widget1.cpp:(.text$_ZN15ProgramSettings17setCurrentSezioneE7QString[ProgramSettings::setCurrentSezione(QString)]+0x10): undefined reference to ProgramSettings::currentSezione' ./debug\widget1.o:widget1.cpp:(.text$_ZN15ProgramSettings17setCurrentSezioneE7QString[ProgramSettings::setCurrentSezione(QString)]+0x20): undefined reference toProgramSettings::currentSezione'
    ./debug\widget1.o:widget1.cpp:(.text$_ZN15ProgramSettings17setCurrentSezioneE7QString[ProgramSettings::setCurrentSezione(QString)]+0x30): undefined reference to ProgramSettings::currentSezione' ./debug\programsettings.o: In functionProgramSettings':
    C:\Users\7-Spode\Documents\Qt-project\Wortfluss-build-simulator-Simulator_Qt_for_MinGW_4_4__Qt_SDK__Debug/../Wortfluss/Utility/programsettings.cpp:11: undefined reference to ProgramSettings::newBigliettoToBeWritten' C:\Users\7-Spode\Documents\Qt-project\Wortfluss-build-simulator-Simulator_Qt_for_MinGW_4_4__Qt_SDK__Debug/../Wortfluss/Utility/programsettings.cpp:12: undefined reference toProgramSettings::currentSezione'
    C:\Users\7-Spode\Documents\Qt-project\Wortfluss-build-simulator-Simulator_Qt_for_MinGW_4_4__Qt_SDK__Debug/../Wortfluss/Utility/programsettings.cpp:11: undefined reference to ProgramSettings::newBigliettoToBeWritten' C:\Users\7-Spode\Documents\Qt-project\Wortfluss-build-simulator-Simulator_Qt_for_MinGW_4_4__Qt_SDK__Debug/../Wortfluss/Utility/programsettings.cpp:12: undefined reference toProgramSettings::currentSezione'
    ./debug\programsettings.o:C:\Users\7-Spode\Documents\Qt-project\Wortfluss-build-simulator-Simulator_Qt_for_MinGW_4_4__Qt_SDK__Debug/../Wortfluss/Utility/programsettings.cpp:40: undefined reference to ProgramSettings::newBigliettoToBeWritten' ./debug\programsettings.o:C:\Users\7-Spode\Documents\Qt-project\Wortfluss-build-simulator-Simulator_Qt_for_MinGW_4_4__Qt_SDK__Debug/../Wortfluss/Utility/programsettings.cpp:45: undefined reference toProgramSettings::newBigliettoToBeWritten'
    ./debug\programsettings.o:C:\Users\7-Spode\Documents\Qt-project\Wortfluss-build-simulator-Simulator_Qt_for_MinGW_4_4__Qt_SDK__Debug/../Wortfluss/Utility/programsettings.cpp:46: undefined reference to ProgramSettings::newBigliettoToBeWritten' ./debug\widgetbiglietti.o:widgetbiglietti.cpp:(.text$_ZN15ProgramSettings17getCurrentSezioneEv[ProgramSettings::getCurrentSezione()]+0x10): undefined reference toProgramSettings::currentSezione'
    @



  • Static members should be defined outside constructor, because the are not connected with any instance of class. For instance you have "here":http://stackoverflow.com/questions/3531060/how-to-initialize-a-static-member-in-c information how to initialize such variable.



  • Just move the static member initialization outside of the class, somewhere in the cpp.

    @QString ProgramSettings::currentSezione = "bla bla";@



  • nothing:

    @
    ProgramSettings::ProgramSettings(QObject *parent) :
    QObject(parent)
    {
    //vedi metodi in heather
    QString ProgramSettings::currentSezione = "";
    QString ProgramSettings::newBigliettoToBeWritten = "";
    }
    @

    errors:
    @
    ..\Wortfluss\Utility\programsettings.cpp: In constructor 'ProgramSettings::ProgramSettings(QObject*)':
    ..\Wortfluss\Utility\programsettings.cpp:11: error: invalid use of qualified-name 'ProgramSettings::currentSezione'
    ..\Wortfluss\Utility\programsettings.cpp:12: error: invalid use of qualified-name 'ProgramSettings::newBigliettoToBeWritten'
    @



  • Static class variables should be initialized outside all methods:

    @
    //vedi metodi in heather
    QString ProgramSettings::currentSezione = "";
    QString ProgramSettings::newBigliettoToBeWritten = "";

    ProgramSettings::ProgramSettings(QObject *parent) :
    QObject(parent)
    {
    }@

    or

    @
    //vedi metodi in heather
    QString ProgramSettings::currentSezione;
    QString ProgramSettings::newBigliettoToBeWritten;

    ProgramSettings::ProgramSettings(QObject *parent) :
    QObject(parent)
    {
    }@



  • [quote author="spode" date="1348582503"]nothing:
    [/quote]

    Well, you are still initializing the strings in the constructor, I said outside of the class in the cpp. You only changed the assignment parameter from a QString to a string literal (const char*). You cannot use static members if they are not defined, you try to define them in the constructor, which counts for usage, which is not possible if they are not defined.



  • solved. thank you so much! =)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.