Undefined reference to external variable

  • I declared here an extern variable:
    extern QString filename;
    I defined here: open.cpp
    void open::openfile()
    QString filename=QFileDialog::getOpenFileName(
    QObject::tr("Open File"),
    QFile file(filename);
    QByteArray ba=file.readAll();


    And i want to use here like this:
    But i got this error: undefined refernce to 'filename'
    What am i doing wrong?

  • Moderators


    in your code, filename is a local variable inside your openfile() function. For it to be an extern variable, you need to define it outside all functions:

    QString filename;

    void open::openfile()
    filename = ...

    However, consider redesigning your program. Minimize your use extern/global variables to make your code more maintainable -- make filename a member variable of an object instead.

Log in to reply

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