1

I am trying to direct the Qt debugging messages into a text log file. while trying to install the message handler function i have written, the following error occurred:

main.cpp: error: C3867: 'Log::myMessageHandler': function call missing argument list; use '&Log::myMessageHandler' to create a pointer to member

please note that :

  1. I have declared the message handler function myMessageHandler as a member function in the singleton Logger class.

  2. The myMessageHandler function was accessed using the static method ReturnInstance().

  3. The function myMessageHandler was installed in the main function of my application using qInstallMessageHanler() function.

The code is as follows:

/****************************Log.h***********************/    
class Log : public QObject
{
    Q_OBJECT
public:
    //class member declaration 

    static Log& ReturnInstance()
    {
       static Log Log_obj;   //The only Log object that will be used
       return Log_obj;
    }
    void myMessageHandler(QtMsgType type,QMessageLogContext &context,const QString &msg);
    private:
    //some member variable declartion
    QFile m_file;

};
/****************************Log.cpp***********************/
//constructor definition
//destructor definition
void Log::myMessageHandler(QtMsgType type, QMessageLogContext &context, const QString &msg)
{

QTextStream ostream(&m_file);

switch (type) {
case QtDebugMsg:
    ostream<<("Debugging Message: %s (%s:%u, %s)\n"  , msg, context.file, context.line, context.function);
    break;
case QtInfoMsg:
    ostream<<("Information Message: %s (%s:%u, %s)\n", msg, context.file, context.line, context.function);
    break;
case QtWarningMsg:
    ostream<<("Warnning Message: %s (%s:%u, %s)\n"   , msg, context.file, context.line, context.function);
    break;
case QtFatalMsg:
    ostream<<("Fatal Message: %s (%s:%u, %s)\n"      , msg, context.file, context.line, context.function);
    break;
case QtCriticalMsg:
    ostream<<("Critical Message: %s (%s:%u, %s)\n"   , msg, context.file, context.line, context.function);
    break;
default:
    break;
  }
}

And here is the installation of the message handler function:

#include "mainwindow.h"
#include <QApplication>
#include "logger.h"

int main(int argc, char *argv[])
{
    Log::ReturnInstance();         
    qInstallMessageHandler(Log::ReturnInstance().myMessageHandler);    //<------ The error
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}
AAEM
  • 1,837
  • 2
  • 18
  • 26
  • 1
    Possible duplicate of [How do I use bind to pass a member function as a function pointer?](https://stackoverflow.com/questions/32583771/how-do-i-use-bind-to-pass-a-member-function-as-a-function-pointer) – RA. Jan 20 '18 at 00:51

1 Answers1

3

You are getting that error because you are trying to pass a pointer to a member function into qInstallMessageHandler(). You really need to pass either (1) a static member or (2) a globally scoped non-member function.

The error message is a bit cryptic, but the "use '&Log::myMessageHandler' to create a pointer to member" part gives a hint of the problem. A member function pointer really needs an object (hidden this pointer) to be called.

Try something more like this:

Method 1: Static member function

class Log : public QObject
{
    Q_OBJECT

public:
    static void myMessageHandler(QtMsgType type,QMessageLogContext &context,const QString &msg)
    {
        // message handling code...
    }

private:
    //some member variable declartion
    QFile m_file;
};

Method 2: Global function

static void myMessageHandler(QtMsgType type, QMessageLogContext &context, const QString &msg)
{
    // message handling code...
}

main.cpp

int main(int argc, char *argv[])
{
    //...

    qInstallMessageHandler(Log::myMessageHandler);
    // OR
    qInstallMessageHandler(myMessageHandler);

    //...
}
Azeem
  • 11,148
  • 4
  • 27
  • 40
Marker
  • 972
  • 6
  • 9
  • I have made it static as mentioned above, but the error error: C2597: illegal reference to non-static member 'Log::m_file' appeared. I tried to solve it by making the QFile m_file static one , this reduced the error to main.obj:-1: error: LNK2001: unresolved external symbol "private: static class QFile Log::m_file" (?m_file@Log@@0VQFile@@A) – AAEM Jan 20 '18 at 10:35
  • When you add a static variable to a class, you also have to declare it outside the class: // Header File (MyClass.h) class MyClass { private: QFile m_file; } // Source File (MyClass.cpp) #include "MyClass.h" QFile MyClass::m_file; – Marker Jan 20 '18 at 14:02