Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. problem in function call
Qt 6.11 is out! See what's new in the release blog

problem in function call

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    saber
    wrote on last edited by
    #1

    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??

    Taz742T 1 Reply Last reply
    0
    • S saber

      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??

      Taz742T Offline
      Taz742T Offline
      Taz742
      wrote on last edited by Taz742
      #2

      @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;
      

      Do what you want.

      S 1 Reply Last reply
      7
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

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

        but you call it without the mode params.

        1 Reply Last reply
        2
        • Taz742T Taz742

          @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;
          
          S Offline
          S Offline
          saber
          wrote on last edited by
          #4

          @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

          Taz742T 1 Reply Last reply
          0
          • S saber

            @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

            Taz742T Offline
            Taz742T Offline
            Taz742
            wrote on last edited by
            #5

            @saber Can you post CommandUtil .h file?

            Do what you want.

            S 1 Reply Last reply
            0
            • Taz742T Taz742

              @saber Can you post CommandUtil .h file?

              S Offline
              S Offline
              saber
              wrote on last edited by
              #6

              @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
              
              
              Taz742T 1 Reply Last reply
              0
              • S saber

                @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
                
                
                Taz742T Offline
                Taz742T Offline
                Taz742
                wrote on last edited by
                #7

                @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

                Do what you want.

                1 Reply Last reply
                3

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved