Click here to Skip to main content
15,937,779 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The function in question accepts three arguments.
The first one is the (GUI) object to be worked on and has no issues
at present this argument is fixef to QTextEdit, but need to be variable in the final version of the function.
the second is an optional function (method) of the object and that works fine if it does not require parameters
It fails with an error - posted down the line
when it REQUIRES a text parameter ( not a pointer )
the third argument is irrelevant at this point.

It appears that compiler does not recognize the argument passed to the second function argument.

The "upper arrow" in the error message clearly indicates problem with the argument itself !

Could somebody please help me to understand this error.

What I have tried:

I do not understand why I am getting the error AND HOW to correct it.
Updated 8-Jun-24 3:57am
Richard MacCutchan 8-Jun-24 11:24am    
Please show the actual code you are referring to and any error messages associated with it.

You have:
QString TRACE_Function( QTextEdit *widget,            // parameter #1
                        void (QTextEdit::*method)(),  // parameter #2
                        QString text_paramater);      // paramater #3

Parameter #2 is defend as a pointer to a function which takes no arguments and returns void.

Your call to TRACE_Function is
pRETC_TEST->TRACE_Function( ui->textEdit,    //argument #1
                            &QTextEdit::append("Trace INITIALIZE"),  //argument #2
                           "TRACE initialize "); //argument #3

Here, argument #2 is the address of whatever the function call to QTextEdit::append("Trace INITIALIZE") returns. Probably you just want
                             "TRACE initialize ");
But note that you'll still get an error, since QTextEdit::append takes an argument, I'm guessing of type QString. Which means your function definition is probably incorrect and should be
QString TRACE_Function(QTextEdit *widget,
                       void (QTextEdit::*method)(Qstring),
                       QString text_paramater );
You'll have to do some digging to see exactly what the argument type to the function pointer should be: I.E. pointer, reference, const, etc.
Share this answer
k5054 6-Jun-24 16:31pm    
Go back and re-read my solution. In particular pay attention to the call to TRACE_Function when wanting to use QTextEdit::append
I've reformatted that part to make things a little more clear
Where is TRACE_Function defined? From your call to pRETC_TEST->TRACE_Function it should be a member of some class, and pRETC_TEST should point to an instantiated object of that class, as per my answer to your question in the C++ forum: Re: BUMP Re: Passing a class (pointer) to library - Linux - C / C++ / MFC Discussion Boards[^].

The error message you show suggests that pRETC_TEST is not a pointer to an object of the class that includes TRACE_Function as one of its non-static methods. However it is not really clear and detailed enough; you need to provide full details of the class, and the methods that you are trying to call.

Here is a simple example of using a method pointer:
#include <iostream>

// QTextEdit is a class that contains a static function
// which outputs a message
class QTextEdit
    QTextEdit() {}
    static void Debug(const char *debugText);

// the implementation of the Debug method
void QTextEdit::Debug(const char *debugText)
    std::cout << "Some debug information: ";
    std::cout << debugText << std::endl;

// this class contains a single function 
class REG_EXP_Test_Class
    void TRACE_Function(void(*method)(const char *msg), const char *parameter);

// the implementation of TRACE_Function takes a pointer to
// a method (which takes a single parameter, and returns nothing)
// and a pointer to a string. It then calls the delegated method
// passing the string, in order to print some information
void REG_EXP_Test_Class::TRACE_Function(void(*method)(const char *msg), const char *parameter)
    method(parameter);  // call the passed in method, via its pointer

int main()
    REG_EXP_Test_Class *pREG_EXP_Test_Class = new REG_EXP_Test_Class();
    // call the TRACE_Function passing it the delegate function
    // and the text to be printed.
    // Note that Debug must be a static method in its class
    pREG_EXP_Test_Class->TRACE_Function(QTextEdit::Debug, "Hello World!");

    return 0;

Try replacing the call to pREG_EXP_Test_Class->TRACE_Function in main with the following:
QTextEdit* pTextEdit = new QTextEdit();
pREG_EXP_Test_Class->TRACE_Function(pTextEdit->Debug, "Hello World!");

Does it still work? Now try removing the word static from the declaration of QTextEdit's Debug method. Pay particular attention to the error messages that you now see.

Share this answer
[no name] 7-Jun-24 8:13am    
These are TWO methods of QTextEdit object - clear and append - append REQUIRES parameter and fails. clear works as expected. The TWO distinct functions are defined similarity, append fails.
Richard MacCutchan 7-Jun-24 8:54am    
I have tried to make all that into a compilable source module but it is too much of a mess. There are parts missing (e.g. where pRETC_TEST is initialised), and you have an if clause in your REG_EXP_Test_Class::TRACE_Function function that will not even compile. You need to edit your question and (using proper <pre> tags) post all the relevant code, in the proper sequence, along with the actual error messages. But before posting please remove all the commented out lines, which just serve to confuse.
[no name] 7-Jun-24 9:52am    
I need to go back to this
You'll have to do some digging to see exactly what the argument type to the function pointer should be: I.E. pointer, reference, const, etc.
[no name] 7-Jun-24 11:21am    
append will not accept QString as pointer
Richard MacCutchan 7-Jun-24 11:59am    
That's because it is defined as taking a const QString& (i.e. a reference) as shown at QTextEdit Class | Qt Widgets 6.7.1[^].
Let me try again. Consider:
int foo(int n) { return 2 * n; }

void bar( int(fn)(int), int param )
   int x = fn(param);
   std::cout << x << std::endl;

That should be pretty clear. We have a function foo that returns an int, and a function bar that takes a pointer to a function, and a parameter. bar calls the fn with the passed in parameter and prints out the result. Simple, straight forward, right?
Now if we do this:
bar(foo, 2)
All is Good. The compiler is happy, code is generated, and when the generated program is run 4 will be printed to stdout when that statement is executed. Everybody's happy, we get to go home on time. Bonuses all round for the team. Yay!

However if we do this:
bar(foo(2), 2)
All is Not Good. The compiler is unhappy, no code is generated. No one is happy, we have to stay late and no bonuses are paid. Can you spot the difference? In the second instance, what is the type of the first argument? Hint: It's not a pointer to a function.

Read, think, reread as necessary. See if this can lead you to enlightenment.
Share this answer
Richard MacCutchan 7-Jun-24 14:46pm    
Look again at the code I posted in my solution, you can compile and link it into an executable and then execute it to see if it works. And that's what we need from you: a minimal reproducible example of the problem complete with listings showing compilation/linking/execution error messages. Posting random lines out of context leaves us in the dark as to how it fits together.
0x01AA 7-Jun-24 15:17pm    
Calm down! You are now on report no. 6 for abvusive reactions!
[no name] 8-Jun-24 14:15pm    
Thanks very much for resolving this issue.
Could not have done it without everybody's help and I appreciate that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900