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

File is not defined.



  • In my project, i have already included a Info.cpp file under my Sources folder but it still gives the error,

    ReferenceError: Info is not defined
    

    And i can see the file included inside my .pro file already.

    How can I get out of this error?



  • @JennyAug13 hi,
    when i have this kind of errors i usually do this combo :

    • close the project
    • delete your projects build folder
    • delete your .pro.user file
    • open the .pro with qtCreator
    • re-configure / qmake


  • I did as follows, but getting the same error.



  • @JennyAug13 Do you have Info.h also?
    are you including Info in the file where you use it ?

    #include "Info.h"
    


  • Yes I have included it already



  • Also i made a .pri file and also added the sources into it


  • Moderators

    @JennyAug13
    can you show us your *.pro file and where you try to include the info.h ?



  • 0_1544780884821_Forums2.JPG

    At the end of my .pro file



  • Following is the .pro file being used

    QT += quick
    CONFIG += c++11
    
    DEFINES += QT_DEPRECATED_WARNINGS
    
    
    SOURCES += \
            main.cpp \
           Info.cpp
    
    RESOURCES += qml.qrc
    
    # Additional import path used to resolve QML modules in Qt Creator's code model
    QML_IMPORT_PATH =
    
    # Additional import path used to resolve QML modules just for Qt Quick Designer
    QML_DESIGNER_IMPORT_PATH =
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    
    

  • Moderators

    @JennyAug13 and main.cpp and info.h and info.cpp are all in the same folder, on your drive?



  • Yes, all are in same folder



  • @JennyAug13
    I think then that you need to show the actual content of your Info.h. According to what you have said the compiler is actually finding the file --- else you would have had a "no such include file" error --- so a message of ReferenceError: Info is not defined would imply that at whatever line that occurs (assuming that source file has already met the necessary included Info.h by that point) the .h has not actually defined the Info class correctly.... You might also like to show us the actual source line which generated that error, together with "proof" that Info.h has been included prior to the line....

    P.S.
    Do ensure you have done a "clean" rebuild. This kind of error does crop up when, say, you have added an include file to a source file but not properly rebuilt, and the compiler uses cached files for the previous state where the header had not been included....


  • Qt Champions 2017

    Looks like somewhere you are doing the cycle.

    1. Info.h is included in one.h.
    2. one.h is included in two.h and
    3. two.h includes info.h again.


  • @dheerendra
    Would your situation actually result in ReferenceError: Info is not defined? I would have thought it would be more like "duplicate definition found"?

    If this does turn out to be the case, the OP needs to put in #ifndef guards to prevent re-inclusion.



  • @dheerendra Nope, it is included only once... inside my info.cpp file and i have only one header file and one source file as of now (Info.cpp and Info.h)


  • Qt Champions 2018

    That's a JS/QML error, where does it occur and can you share some code ?



  • This is the actual error:

    qrc:/Theme/Icons.qml:33: ReferenceError: Info is not defined
    qrc:/Theme/Font.qml:53: ReferenceError: Info is not defined
    

    and in the lines of these files, following lines of code is there

    @qrc:/Theme/Icons.qml:33:   
    
    readonly property string imagePath: Info.getQMLResourcePath() + "Theme/Images/"
    
    @qrc:/Theme/Font.qml:53:
    
    source: Info.getQMLResourcePath() + "Theme/Fonts/ProLight.otf";
    
    

  • Qt Champions 2018

    Did you use qmlRegisterSingletonType on your Info type ?
    And then added an import line for the module in your qml file ?



  • @GrecKo said in File is not defined.:

    That's a JS/QML error

    good catch



  • Here is the code inside my "Info.cpp" file for your reference.

    // own header
    #include "Info.h"
    #include <QCoreApplication>
    #include <QQmlEngine>
    #include <QDir>
    
    Q_LOGGING_CATEGORY(LC_Info, "Info")
    
    // Root direcoty
    const QString Info::ASSETS_FOLDER_NAME                = "assets";
    
    //// System & User directories
    const QString Info::SYSTEM_ASSETS_FOLDER_NAME         = "system";
    const QString Info::USER_ASSETS_FOLDER_NAME           = "user";
    const QString Info::SETTINGS_ASSETS_FOLDER_NAME       = "settings";
    const QString Info::RESOURCE_ASSETS_FOLDER_NAME       = "resources";
    
    //// System directories
    const QString Info::THERAPY_ASSETS_FOLDER_NAME        = "therapies";
    const QString Info::LANGUAGE_ASSETS_FOLDER_NAME       = "lang";
    
    //// User directories
    const QString Info::STATISTICS_FOLDER_NAME            = "statistics";
    const QString Info::EMG_DATA_FOLDER_NAME              = "emg-data";
    const QString Info::FAVORITES_FOLDER_NAME             = "favorites";
    const QString Info::PATIENT_PROGRAMS_FOLDER_NAME      = "patient-programs";
    const QString Info::LOG_FOLDER_NAME                   = "log";
    
    //// Static filenames
    const QString Info::DEFAULT_THERAPY_VALUES_FILE_NAME  = "therapies.json";
    
    Info::Info()
      : QObject(),
        m_buildInQmlResource(true)
    {
      QString     envAssetsPath = qgetenv("ASSETS_PATH");
      QStringList assetsSearchPaths;
    
      if (!envAssetsPath.isEmpty()) {
        assetsSearchPaths.append(envAssetsPath);
      }
    
      if (QCoreApplication::instance() != nullptr) {
        assetsSearchPaths.append(QCoreApplication::applicationDirPath());
      }
      assetsSearchPaths.append(QDir::currentPath());
    
      foreach (QString path, assetsSearchPaths) {
    
        QDir d(path + QDir::separator() + ASSETS_FOLDER_NAME);
    
        // Found Assets folder:
        if (d.exists()) {
          m_assetsPath = d.absolutePath() + QDir::separator();
          break;
        }
      }
    
      // User assets directory name can be adjusted through environment
      // variable. This is mainly for defining a spearate path for testing
      // purposes (e.g. unit tests)
      QByteArray envUserAssetsName = qgetenv("USER_ASSETS_NAME");
      m_userAssetsFolderName = envUserAssetsName.isEmpty() ?
            USER_ASSETS_FOLDER_NAME :
            envUserAssetsName;
    }
    
    bool Info::createPath(const QString & path)
    {
      QDir d(path);
      if (d.exists()) {
        return true;
      }
      return d.mkpath(path);
    }
    
    Info *Info::getInstance()
    {
      static Info info;
      return &info;
    }
    
    QObject *Info::getInstanceQml(QQmlEngine *engine, QJSEngine *scriptEngine)
    {
      Q_UNUSED(engine)
      Q_UNUSED(scriptEngine)
    
      QObject * inst = Info::getInstance();
    
      QQmlEngine::setObjectOwnership(inst, QQmlEngine::CppOwnership);
    
      return inst;
    }
    
    QString Info::getUserPath()
    {
      return getInstance()->m_assetsPath +
          getInstance()->m_userAssetsFolderName +
          QDir::separator();
    }
    
    QString Info::getUserSettingsPath()
    {
      return getInstance()->getUserPath() +
          Info::SETTINGS_ASSETS_FOLDER_NAME +
          QDir::separator();
    }
    
    QString Info::getStatisticsPath()
    {
      return getInstance()->getUserPath() +
          Info::STATISTICS_FOLDER_NAME +
          QDir::separator();
    }
    
    QString Info::getEmgDataPath()
    {
      return getInstance()->getUserPath() +
          Info::EMG_DATA_FOLDER_NAME +
          QDir::separator();
    }
    
    QString Info::getFavoritesPath()
    {
      return getInstance()->getUserPath() +
          Info::FAVORITES_FOLDER_NAME +
          QDir::separator();
    }
    
    QString Info::getLogPath()
    {
      return getInstance()->getUserPath() +
          Info::LOG_FOLDER_NAME +
          QDir::separator();
    }
    
    QString Info::getPatientProgramsPath()
    {
      return getInstance()->getUserPath() +
          Info::PATIENT_PROGRAMS_FOLDER_NAME +
          QDir::separator();
    }
    
    QString Info::getSystemPath()
    {
      return getInstance()->m_assetsPath +
          Info::SYSTEM_ASSETS_FOLDER_NAME +
          QDir::separator();
    }
    
    QString Info::getAssetsPath()
    {
      return getInstance()->m_assetsPath +
          QDir::separator();
    }
    
    QString Info::getLangPath()
    {
      return getInstance()->getSystemPath() +
          Info::LANGUAGE_ASSETS_FOLDER_NAME +
          QDir::separator();
    }
    
    QString Info::getResourcePath()
    {
      return getInstance()->getSystemPath() +
          Info::RESOURCE_ASSETS_FOLDER_NAME +
          QDir::separator();
    }
    
    QString Info::getQMLResourcePath()
    {
      return getInstance()->m_buildInQmlResource ? "qrc:/" : ("file:/" + Info::getResourcePath());
    }
    
    QString Info::getSystemTherapyPath()
    {
      return getInstance()->getSystemPath() +
          Info::THERAPY_ASSETS_FOLDER_NAME +
          QDir::separator();
    }
    
    QString Info::getSystemSettingsPath()
    {
      return getInstance()->getSystemPath() +
          Info::SETTINGS_ASSETS_FOLDER_NAME +
          QDir::separator();
    }
    
    bool Info::init()
    {
      bool ok = true;
    
      qCDebug(LC_Info) << "************************************";
      qCDebug(LC_Info) << "Initialising Neurotab Assets File System:";
    
      qCDebug(LC_Info) << "Assets directory: " << getInstance()->m_assetsPath;
    
      // Assets path MUST exist!!!
      QDir ntSysPath(getSystemPath());
      if (!ntSysPath.exists()) {
        qCCritical(LC_Info) << "Assets / System directory MUST exist! ("
                                    << getSystemPath()
                                    << ")";
        return false;
      }
    
      if (!createPath(getUserPath())) {
        qCCritical(LC_Info) << "could not create " << getUserPath();
        ok = false;
      }
      if (!createPath(getLogPath())) {
        qCCritical(LC_Info) << "could not create " << getLogPath();
        ok = false;
      }
      if (!createPath(getUserSettingsPath())) {
        qCCritical(LC_Info) << "could not create " << getUserSettingsPath();
        ok = false;
      }
      if (!createPath(getStatisticsPath())) {
        qCCritical(LC_Info) << "could not create " << getStatisticsPath();
        ok = false;
      }
      if (!createPath(getEmgDataPath())) {
        qCCritical(LC_Info) << "could not create " << getEmgDataPath();
        ok = false;
      }
      if (!createPath(getPatientProgramsPath())) {
        qCCritical(LC_Info) << "could not create " << getPatientProgramsPath();
        ok = false;
      }
      if (!createPath(getFavoritesPath())) {
        qCCritical(LC_Info) << "could not create " << getFavoritesPath();
        ok = false;
      }
    
      qCDebug(LC_Info) << "done\n";
    
      return ok;
    }
    
    void Info::resetAllUserAssets()
    {
      QDir d(getUserPath());
      d.removeRecursively();
      init();
    }
    
    void Info::useBuildInQml(bool buildIn)
    {
      getInstance()->m_buildInQmlResource = buildIn;
    }
    

  • Qt Champions 2017

    This class looks great. This is the c++ claas. If u want to see this class at qml side, it needs to be registered to qml using qmlregistertype etc methods. Looks like your class is not exposed to qml. Please check and if not please do so. Search in Google or qt assistant for how to expose c++ class to qml.



  • I have registered to qml using qmlregistertype method and included it inside my qml file as follows. But it gives following errors when i build.

    inside my main.cpp file

    qmlRegisterType <Info>("NT.Cpp",1,0, "Info");
    

    inside my Icons.qml file it is included as

    import NT.Cpp 1.0
    

    But many errors appear as follows:

    /opt/Qt/5.6.3/gcc_64/include/QtQml/qqmlprivate.h:93: error: within this context
         class QQmlElement : public T
               ^
    
    /Info.h:98: error: ‘Info::Info()’ is private
         Info();
         ^
    /opt/Qt/5.6.3/gcc_64/include/QtQml/qqmlprivate.h:93: ‘QQmlPrivate::QQmlElement<Info>::QQmlElement()’ is implicitly deleted because the default definition would be ill-formed:
         class QQmlElement : public T
               ^
    
    

  • Qt Champions 2017

    Looks like you have private constructor in Info class. Can you show the info.h file ? Above post only has implementation file.


  • Qt Champions 2018

    Yes it looks like a private constructor, and with a getInstance method to get the singleton instance.

    Use qmlRegisterSingletonType and not qmlRegisterType. If you use this instance in c++ too, don't forget to set the object ownership (with setObjectOwnership) to QQmlEngine::CppOwnership.


Log in to reply