problem in function call
Solved
General and Desktop
-
i am not able to call a function .
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;
-
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