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

problem in function call



  • i am not able to call a function .
    0_1525432539892_fr.png

    this is the function.

    QString CommandUtil::readStringFromFile(const QString &path, const QIODevice::OpenMode &mode)
    {
        QSharedPointer<QFile> file(new QFile(path));
    
        QString data;
    
        if(file->open(mode)) {
    
          data = file->readAll();
    
          file->close();
        }
    
        return data;
    }
    

    i called that function

    quint64 NetworkInfo::getTXbytes() const
    {
        quint64 tx = CommandUtil::readStringFromFile(txPath)
                .trimmed()
                .toLong();
    
        return tx;
    }
    

    got this error

    /home/shaber/Desktop/Folder/work/corebox/dashboard/lib/network_info.cpp:39: error: no matching function for call to ‘CommandUtil::readStringFromFile(const QString&)’
         quint64 tx = CommandUtil::readStringFromFile(txPath)
                                                            ^
    

    what i am doing wrong??



  • @saber
    Because function must get 2 parameters, but you send 1.
    If you don't want to send parameter, you can add a default OpenMode only in your .h file.

    For example

    static QString CommandUtil::readStringFromFile(const QString &path, const QIODevice::OpenMode &mode = QIODevice::ReadOnly);
    
    

    or other open mode from http://doc.qt.io/qt-5/qiodevice.html#OpenModeFlag-enum, or just send a parameter.

    static QString CommandUtil::readStringFromFile(const QString &path, const QIODevice::OpenMode &mode = QIODevice::ReadOnly);
    

    Are you interested what does it mean?
    If you call this function without mode parameter:

    CommandUtil::readStringFromFile("fileAddress.txt"); //by default mode = QIODevice::ReadOnly;
    

    If you call this function with mode parameter:

    CommandUtil::readStringFromFile("fileAddress.txt", QIODevice::WriteOnly); //now mode = QIODevice::WriteOnly;
    

  • Lifetime Qt Champion

    As @Taz742 says
    the function is
    commandUtil::readStringFromFile(const QString &path, const QIODevice::OpenMode &mode)

    but you call it without the mode params.



  • @Taz742 @mrjj i have no problem with that .
    i moved the "readStringFromFile " function from "FileUtil" to "CommandUtil" .
    when i called the function from "CommandUtil" , it shows that error .

    0_1525495139847_hu.png



  • @saber Can you post CommandUtil .h file?



  • @Taz742

    command_util.h

    #ifndef COMMAND_UTIL_H
    #define COMMAND_UTIL_H
    
    #include "stacer-core_global.h"
    
    #include <QStringList>
    #include <QFile>
    #include <QDir>
    #include <QTextStream>
    #include <QDirIterator>
    #include <QStandardPaths>
    #include <QSharedPointer>
    
    class STACERCORESHARED_EXPORT CommandUtil
    {
    
    public:
        static QString sudoExec(const QString &cmd, QStringList args = QStringList());
        static QString exec(const QString &cmd, QStringList args = QStringList());
        static bool isExecutable(const QString &cmd);
    
        static QString readStringFromFile(const QString &path, const QIODevice::OpenMode &mode);
        static QStringList readListFromFile(const QString &path, const QIODevice::OpenMode &mode);
    private:
        CommandUtil();
    
    };
    
    #endif // COMMAND_UTIL_H
    
    


  • @saber
    Did you read my answer ?

    @saber said in problem in function call:

    static QString readStringFromFile(const QString &path, const QIODevice::OpenMode &mode);
    static QStringList readListFromFile(const QString &path, const QIODevice::OpenMode &mode);
    

    change const QIODevice::OpenMode &mode to const QIODevice::OpenMode &mode = QIODevice::ReadOnly


Log in to reply